【发布时间】:2015-05-18 14:09:10
【问题描述】:
在下面的代码中,我公开了一个 POST 端点。 POST 的返回值由 Success 对象上的布尔属性确定:
@RequestMapping(value="/new", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String createDispatch(@RequestParam(value="p1") String p1) {
Success ds = new Success();
//Set a boolean on Success object to true
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(ds);
} catch (JsonGenerationException e) {
e.printStackTrace();
return {"exception": "true"};
} catch (JsonMappingException e) {
e.printStackTrace();
return {"exception": "true"};
} catch (IOException e) {
e.printStackTrace();
return {"exception": "true"};
}
}
在写入转换为 JSON (return mapper.writeValueAsString(ds)) 时捕获每种异常类型似乎是多余的,好像创建 JSON 字符串时存在问题(匹配异常子句之一)然后我无法返回问题所在客户端,因为我无法将异常包装在 Json 对象中。所以我只返回 return {"exception": "true"}; 。
我应该返回什么来封装应该抛出的异常类型?
【问题讨论】:
标签: java spring post jackson endpoints