[英]Exception full class name is coming in Rest API json response
I have written a code which will throw the following message if the list id specify in the URL didn't find in the db.我编写了一个代码,如果在 db 中找不到 URL 中指定的列表 ID,它将抛出以下消息。 It should send a json response with error message but i am getting exception class name also with message:它应该发送一个带有错误消息的 json 响应,但我也收到了带有消息的异常类名:
Expected Output from rest API:其余 API 的预期输出:
{
"code": 404,
"message": "Watchlist dnd was not found"
}
code:代码:
@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;
}
But I am getting this response:但我得到这个回应:
{
"code": 404,
"message": "class com.ibm.cedm.exception.NotFoundException:Watchlist dnd was not found"
}
anybody know why is it so ?有人知道为什么会这样吗?
As you can see in the reference: https://docs.oracle.com/javaee/7/api/javax/ws/rs/NotFoundException.html#NotFoundException-java.lang.String- , when you pass the error message to a NotFoundException, surely the Throwable.getMessage()
enrich that message with the class name.正如您在参考中看到的: https : //docs.oracle.com/javaee/7/api/javax/ws/rs/NotFoundException.html#NotFoundException-java.lang.String- ,当您将错误消息传递给一个 NotFoundException,当然Throwable.getMessage()
用类名丰富了该消息。
NotFoundException
also accepts a Response
as parameter, so instead of passing a message you can build the response by passing the Response
. NotFoundException
也接受Response
作为参数,因此您可以通过传递Response
来构建响应,而不是传递消息。
final Response.ResponseBuilder response = Response.status(Response.Status.NOT_FOUND);
response.entity("Watchlist " + listId + " was not found").type("text/plain");
throw new NotFoundException(response.build());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.