简体   繁体   English

Rest API json 响应中出现异常完整类名

[英]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.

相关问题 POSTMAN 中的 REST API JSON 响应:响应中的一个字段为空 - REST API JSON response in POSTMAN: one field is coming as null in response 如何在 Spring Boot 中使用 RestTemplate 从来自外部 REST API 的 JSON 响应中提取数据? - How to extract data from JSON response coming from external REST API using RestTemplate in Spring Boot? REST API 和 Glassfish 中的 JSON 响应 - JSON response in REST API and Glassfish java sql 时间戳以毫秒形式出现在 rest api 响应中 - java sql timestamp coming as milliseconds in rest api response 在API响应中的dropwizard中找不到类org.json.JSON的序列化程序,但在打印返回类型时结果进入 - No serializer found for class org.json.JSON in dropwizard in API Response but Result is Coming in when am printing a return type Rest API响应(异常/错误/默认) - Rest API Response (Exception/Error/Default) 放心检查名称是否存在于json响应中 - rest assured check the name is exist in the json response Java Spring Boot REST API **How to name the array of objects in HTTP json response? - Java Spring Boot REST API **How to name the array of objects in HTTP json response? 解析来自 REST API 的 json 响应,其字段名称为 id - Parsing of json response from REST API which has id as field name Java将JSON Rest API响应转换为图像 - Java convert json rest api response to image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM