【发布时间】:2016-09-11 19:36:43
【问题描述】:
我在 localhost:8080 上运行我的服务器。我想生成一个与 restaurantImage 相关联的餐厅列表。我正在本地驱动器中上传带有 ID 的图像。我的上传状态是:
{
"uploadstatus": "C:/data/5731453cf1a60921d4d1b8b8oracle.JPG"
}
当我得到这个列表时,我从我的控制器中找到了这些。
{
"total": 1,
"list": [
{
"id": "5731453cf1a60921d4d1b8b8",
"name": "Kualalam",
"address": "kkkk",
"restaurantImage": "C:/data/5731453cf1a60921d4d1b8b8oracle.JPG",
"review": null
}
]
}
这是我用来获取响应的代码:
@GET
@Produces("application/json")
public Response list() {
Map<Object, Object> apiResponse = new HashMap<Object, Object>();
Map<Object, Object> serviceResponse = new HashMap<Object, Object>();
try {
List<Restaurant> restaurants = restaurantService.list();
serviceResponse.put("total", restaurants.size());
serviceResponse.put("list", restaurants);
return Response.ok(serviceResponse).build();
} catch (Exception e) {
logger.error("Error in getting restaurants list:", e);
apiResponse.put("error", e.getMessage());
}
return Response.status(500).entity(apiResponse).build();
}
我在这里想要什么,我想将这家餐厅的图片设为 http://localhost:8080/data/5731453cf1a60921d4d1b8b8oracle.JPG 。我如何配置我的控制器以实现所需的输出?
【问题讨论】:
-
您的响应似乎是
java.util.HashMap的类型,您希望如何获得URL (java.net.URL) 类型的Response? -
我现在正在尝试这样:List
Restaurants = restaurantService.list(); for(餐厅餐厅: 餐厅) { String imageURI = restaurant.getRestaurantImage(); restaurant.setRestaurantImage(AppConstant.RESTAURANT_IMG_URL+imageURI); } -
您尝试获取路径 AbsolutePath 并将其设置为您的 serviceResponse 我猜。但是单线程不会让您读取路径并写入地图,您应该尝试观察者模式来获取绝对路径,然后将其写入您的 HashMap。
-
你能帮我写一些sn-p代码吗?
-
我没有足够的时间提供代码 sn-p 但我有一个链接 (java2blog.com/2013/02/observer-design-pattern-in-java.html) 可以有效地描述这种情况。