java - how to send images from a RESTful web service?

José María

I'm implementing a Restful Web Service on Java, with no framework. My idea is to send images from a file server, because storing them on a database slows the server down. By the moment, I have the following code, which returns json content:

@Path("articulo")
public class ArticuloResource {

    @Context
    private UriInfo context;
    private final ArticuloService service;
    private final Gson gson;

    /**
     * Creates a new instance of ArticuloResource
     */
    public ArticuloResource() {
        this.service = new ArticuloService();
        this.gson = new Gson();
    }

    /**
     * Retrieves representation of an instance of ArticuloResource
     * @return an instance of com.tienda.rest.pojo.Articulo
     */
    @GET
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getJson() {
        return this.gson.toJson(this.service.seleccionarTodo());
    }    

    @GET
    @Path("novedades")
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getNovedades() {
        return "Novedades";
    }

    @GET
    @Path("{id}")
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getArticulo(@PathParam("id") Integer id) {
        return this.gson.toJson(this.service.getUnique(id));
    }

    /**
     * PUT method for updating or creating an instance of ArticuloResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public void putJson(Articulo content) {
    }

    @GET
    @Produces(Metodos.Parametros.TYPE_IMAGE_PNG)
    public Response getImage() {
        return null;
    }
}

Ideas? Thanks in advance.

VHS

Try following:

  @GET
    @Produces(Metodos.Parametros.TYPE_IMAGE_PNG)
    public Response getImage() {
        byte[] bytes = Files.toByteArray("file.png");
        return Response.ok(bytes).build();
    }

You can try to stream the image. It might be a bit better. return Response.ok(new ByteArrayInputStream(bytes)).build();

However no matter which option you choose, it's going to be a bit slow. You can send a redirect link to another server which can deliver the image to the client independently of your app server. It is better than sending the image itself in response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to send multiple files in postman ReSTful web service?

From Dev

How to expose a C library to a RESTful web service

From Dev

How to call RESTful web service remotely

From Dev

java Restful web service get the data from textbox in POST action

From Dev

how to call RESTful web service in phonegap application?

From Dev

NullPointerException on Restful Web Service from Database

From Dev

Recursively download resources from RESTful web service

From Dev

populate a jtable with json string results from a RESTful web service using java

From Dev

Use threads on a RESTful Web Service in Java

From Dev

how to Call Restful web service using javaScript?

From Dev

How to enable CORS with a java restful web service on JBoss AS

From Dev

how to consume a Restful Web Service (Restful API) in Java

From Dev

How to expose a cxf restful web service in java?

From Dev

How to get some data from a restful web service, and save it to the database?

From Dev

Java Restful Web Service - Static Member Variables

From Dev

Phonegap app - How to get images from Json Web Service Response?

From Dev

How to call RESTful web service remotely

From Dev

Send XML to RESTful web service from Unity

From Dev

how to send JSON from web service to javascript in iOS projects?

From Dev

how to call RESTful web service in phonegap application?

From Dev

How to send a message from java restful server to gcm server

From Dev

How to enable CORS with a java restful web service on JBoss AS

From Dev

Deploy Java restful web service to live server

From Dev

Java , how can i get JSON from restful(?) web service

From Dev

How to send JSON data as parameter from C# to Jersey RESTful Service

From Dev

How to expose a cxf restful web service in java?

From Dev

How to test a restful web service in java

From Dev

Access SevletContext from interceptor in restful web service

From Dev

java - How to receive a Json object on a Restful web service?

Related Related

  1. 1

    How to send multiple files in postman ReSTful web service?

  2. 2

    How to expose a C library to a RESTful web service

  3. 3

    How to call RESTful web service remotely

  4. 4

    java Restful web service get the data from textbox in POST action

  5. 5

    how to call RESTful web service in phonegap application?

  6. 6

    NullPointerException on Restful Web Service from Database

  7. 7

    Recursively download resources from RESTful web service

  8. 8

    populate a jtable with json string results from a RESTful web service using java

  9. 9

    Use threads on a RESTful Web Service in Java

  10. 10

    how to Call Restful web service using javaScript?

  11. 11

    How to enable CORS with a java restful web service on JBoss AS

  12. 12

    how to consume a Restful Web Service (Restful API) in Java

  13. 13

    How to expose a cxf restful web service in java?

  14. 14

    How to get some data from a restful web service, and save it to the database?

  15. 15

    Java Restful Web Service - Static Member Variables

  16. 16

    Phonegap app - How to get images from Json Web Service Response?

  17. 17

    How to call RESTful web service remotely

  18. 18

    Send XML to RESTful web service from Unity

  19. 19

    how to send JSON from web service to javascript in iOS projects?

  20. 20

    how to call RESTful web service in phonegap application?

  21. 21

    How to send a message from java restful server to gcm server

  22. 22

    How to enable CORS with a java restful web service on JBoss AS

  23. 23

    Deploy Java restful web service to live server

  24. 24

    Java , how can i get JSON from restful(?) web service

  25. 25

    How to send JSON data as parameter from C# to Jersey RESTful Service

  26. 26

    How to expose a cxf restful web service in java?

  27. 27

    How to test a restful web service in java

  28. 28

    Access SevletContext from interceptor in restful web service

  29. 29

    java - How to receive a Json object on a Restful web service?

HotTag

Archive