how can i return response status 405 with empty entity?

偶尔善良 提交于 2019-12-10 20:21:46

问题


How can i return response status 405 with empty entity in java REST?

@POST
@Path("/path")
public Response createNullEntity() {
    return Response.created(null).status(405).entity(null).build();
}

It returns status code 405, but the entity is not null, it is the http page for the error 405.


回答1:


When you return an error status, Jersey delegates the response to your container's error processing via sendError. When sendError is called, the container will serve up an error page. This process is outlined in the Java Servlet Specification §10.9 Error Handling.

I suspect what you are seeing is your container's default error page for a 405 response. You could probably resolve your issue by specifying a custom error page (which could be empty). Alternatively, Jersey won't use sendError if you provide an entity in your response. You could give it an empty string like this:

@POST
@Path("/path")
public Response createNullEntity() {
  return Response.status(405).entity("").build();
}

The above results in Content-Length of 0



来源:https://stackoverflow.com/questions/17428450/how-can-i-return-response-status-405-with-empty-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!