【问题标题】:Exception full class name is coming in Rest API json response异常完整的类名出现在 Rest API json 响应中
【发布时间】: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


    【解决方案1】:

    正如您在参考资料中看到的: https://docs.oracle.com/javaee/7/api/javax/ws/rs/NotFoundException.html#NotFoundException-java.lang.String- ,当您将错误消息传递给 NotFoundException 时,Throwable.getMessage() 肯定会使用类名丰富该消息。

    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2021-04-23
      • 2017-10-06
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多