【发布时间】:2020-04-01 22:26:58
【问题描述】:
我编写了一个代码,如果在 db 中找不到 URL 中指定的列表 ID,它将抛出以下消息。它应该发送带有错误消息的 json 响应,但我也收到异常类名和消息:
来自 REST API 的预期输出:
{
"code": 404,
"message": "Watchlist dnd was not found"
}
代码:
@RolesAllowed({ "admin" })
@Path("/{listId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns a watchlist.", notes = "")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "The watchlist was returned.", response = Watchlist.class),
@ApiResponse(code = 404, message = "The watchlist was not found.", response = ErrorMessage.class),
@ApiResponse(code = 500, message = "Internal server error.", response = ErrorMessage.class) })
public Watchlist getList(@PathParam("listId") String listId, @HeaderParam("x-access-token") String jwtToken,
@Context SecurityContext sec, @Context final HttpServletResponse response) throws Exception {
final String sourceMethod = "getList";
if (logger.isLoggable(Level.FINER)) {
logger.entering(CLASSNAME, sourceMethod);
}
WatchlistService service = new WatchlistService(cedmPersitence);
Watchlist list = service.getWatchList(listId);
if (logger.isLoggable(Level.FINER)) {
logger.exiting(CLASSNAME, sourceMethod);
}
return list;
}
public Watchlist getWatchList(String listId) throws IOException,NotFoundException{
Watchlist list = new Watchlist();
list.setListId(listId);
if(listId !=null) {
HBasePersistence persistence = new HBasePersistence();
persistence.init("watchlist");
List<WatchlistEntry> watchListEntries = persistence.getWatchlistByListId(listId);
if (watchListEntries == null || watchListEntries.isEmpty()) {
throw new NotFoundException("Watchlist " + listId + " was not found");
}
list.setEntries(watchListEntries);
}
return list;
}
但我收到了这样的回复:
{
"code": 404,
"message": "class com.ibm.cedm.exception.NotFoundException:Watchlist dnd was not found"
}
有人知道为什么会这样吗?
【问题讨论】:
标签: java rest web-services postman