简体   繁体   中英

how to get an image in jersey rest web service

My listBo class

public class ListBO {

public String itemName = "";

public String itemType = "";

public String itemImg = "";

public String itemPrice = "";

public int itemId;

    /**
     * @return the itemName
     */

public String getItemName() {

return itemName;

}
    /**
     * @param itemName the itemName to set
     */

public void setItemName(String itemName) {

this.itemName = itemName;

}
    /**
     * @return the itemType
     */

public String getItemType() {

return itemType;

}
    /**
     * @param itemType the itemType to set
     */

public void setItemType(String itemType) {

this.itemType = itemType;

}
    /**
     * @return the itemImg
     */

public String getItemImg() {

return itemImg;

}
    /**
     * @param itemImg the itemImg to set
     */

public void setItemImg(String itemImg) {

this.itemImg = itemImg;

}

public void setItemId(int itemId) {

this.itemId = itemId;

}

public String getItemPrice(){

return itemPrice;


}

public void setItemPrice(String itemPrice) {

this.itemPrice = itemPrice;

}

My listjson class

@Path("/listItems")

public class ListJson {         


@Path("/lists")

@POST

@Produces(MediaType.APPLICATION_JSON)

public List<ListBO> getList(){

System.out.println("Called Main JSON");

List<ListBO> lists = new ArrayList<ListBO>();


for (int i = 0; i < 10; i++) {


ListBO items = new ListBO();


items.setItemId(i);


items.setItemName("Chicken :"+i);


items.setItemType("Type :"+i);


items.setItemPrice("Price :"+i);


if(i<=10){


items.setItemPrice("20");


}


lists.add(items);           



}


return lists;

}

i can add image like this but it doesnot return an image please helpme..

/*@Path("/image") class ImageService {

private static final String FILE_PATH = "c:\\images.jpg";

@GET

@Path("/get")

@Produces("image/jpg")

public Response getFile() {

File file = new File(FILE_PATH);

ResponseBuilder response = Response.ok((Object) file);

response.header("Content-Disposition",

"attachment; filename=image_from_server.jpg");

return response.build();

}

} */

}

how to return an image and where can i store an image give me an example..

you can't return an image in a JSON object. JSON is not meant for binary data. your web service should return JSON containing a field that is a reference to the image, a URL. then the code that consumes the web service must get the URL from the JSON and fetch the image from the URL.

{ imageUrl: "http://foo.com/image.png" }

it's hard to say exactly what you are doing without more information. you also might want to re-tag this as jersey or resteasy, not android. i think the code you posted is a jersey resource, looking at the annotations.

使用BSON在其余Web服务中发送图像。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM