简体   繁体   English

Restful Services:发送HTTP 405而不是HTTP 500错误

[英]Restful Services: Sending HTTP 405 instead of HTTP 500 error

There are two issues: 有两个问题:
Issue 1: 问题1:
When user does not send any request payload for one of the POST api call. 当用户未为POST api调用之一发送任何请求有效负载时。 Its corresponding RestController method, processes the incoming request and throws a null pointer exception. 其对应的RestController方法,处理传入的请求并引发空指针异常。 It sends back HTTP 500 error code and stack trace as the error description. 它发回HTTP 500错误代码和堆栈跟踪作为错误描述。
HTTP 405 is expected to be return here. HTTP 405预计将返回此处。

@POST
@Path("/create")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response createEntity(MyEntity entity) 
{
 EntityRequest req = new EntityRequest();
 req.setName(entity.getName());//throws NPE here
 //few more lines to send req to backend and get the response back
 return response;
}


Issue 2: 问题2:
There is GET request API implementation. GET请求API实现。

@GET
@Path("/entity/{entityId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON})
public Response findMyEntityById(
        @PathParam("entityId") String id) 
{
  EntityRequest req = new EntityRequest();
  //similar stuff and sends the response back
  return response;
}

Issue is if user hit the http://localhost:8080/entities/entity/12 and sends a POST request instead of GET . 问题是用户是否点击了http://localhost:8080/entities/entity/12并发送POST请求而不是GET API should throw 405 but it is throwing 500 right now. API应该抛出405,但现在抛出500。 Actual stack trace: 实际堆栈跟踪:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException and MIME media type application/octet-stream was not found

The first case is clear. 第一种情况很明显。 405 means "Method Not Allowed". 405表示“不允许使用方法”。 It is typically thrown by container or framework if specific URL cannot reply to request of current method. 如果特定的URL无法回复当前方法的请求,则通常由容器或框架抛出。 But you do use correct method but with empty body that causes NPE in your code. 但是,您确实使用了正确的方法,但是主体为空,导致代码中出现NPE。 In this case 500 status is expected. 在这种情况下,预期状态为500。

Issue 2 is not clear. 问题2尚不清楚。 You should send more details. 您应该发送更多详细信息。 You either do not send POST or the URL can accept POST. 您要么不发送POST,要么URL可以接受POST。 Please double check and send details about your configuration, some code snippets etc. 请仔细检查并发送有关您的配置,一些代码段等的详细信息。

EDIT: 编辑:

after posting some code I can say that GET request cannot consume anything. 发布一些代码后,我可以说GET请求不能使用任何东西。 Remove @Consumes({ MediaType.APPLICATION_JSON }) from findMyEntityById() . findMyEntityById()删除@Consumes({MediaType.APPLICATION_JSON} findMyEntityById() Try it. 试试吧。 If it helps you can say that it is a kind of bug in Jersey: it actually should either throw exception during deployment that GET cannot consume anything or ignore @Consume if GET method is processed. 如果有帮助,您可以说这是Jersey中的一种错误:实际上,它应该在部署过程中引发GET无法消耗任何东西的异常,或者在处理GET方法时忽略@Consume

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM