[英]how to get an image in jersey rest web service
My listBo class 我的listBo班
public class ListBO { 公共课程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 { / * @ Path(“ / image”)类ImageService {
private static final String FILE_PATH = "c:\\images.jpg"; 私有静态最终字符串FILE_PATH =“ c:\\ images.jpg”;
@GET @得到
@Path("/get") @Path(“ / get”)
@Produces("image/jpg") @Produces(“ image / jpg”)
public Response getFile() { 公共响应getFile(){
File file = new File(FILE_PATH); 文件文件=新文件(FILE_PATH);
ResponseBuilder response = Response.ok((Object) file); ResponseBuilder响应= Response.ok((目标)文件);
response.header("Content-Disposition", response.header(“ Content-Disposition”,
"attachment; filename=image_from_server.jpg"); “附件; filename = image_from_server.jpg”);
return response.build(); 返回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对象中返回图片。 JSON is not meant for binary data. JSON不适用于二进制数据。 your web service should return JSON containing a field that is a reference to the image, a URL. 您的Web服务应返回JSON,其中包含一个字段,该字段是对图像的引用,一个URL。 then the code that consumes the web service must get the URL from the JSON and fetch the image from the URL. 那么使用Web服务的代码必须从JSON获取URL并从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. 您可能还想将其重新标记为jersey或resteasy,而不是android。 i think the code you posted is a jersey resource, looking at the annotations. 我认为您发布的代码是球衣资源,请查看注释。
使用BSON在其余Web服务中发送图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.