Restful Services:发送HTTP 405而不是HTTP 500错误 [英] Restful Services: Sending HTTP 405 instead of HTTP 500 error

查看:275
本文介绍了Restful Services:发送HTTP 405而不是HTTP 500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个问题:
问题1:
当用户未为POST api调用之一发送任何请求有效负载时.其对应的RestController方法,处理传入的请求并引发空指针异常.它发送回HTTP 500错误代码和堆栈跟踪作为错误描述.
期望HTTP 405返回此处.

There are two issues:
Issue 1:
When user does not send any request payload for one of the POST api call. Its corresponding RestController method, processes the incoming request and throws a null pointer exception. It sends back HTTP 500 error code and stack trace as the error description.
HTTP 405 is expected to be return here.

@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;
}


问题2:
GET请求API实现.


Issue 2:
There is GET request API implementation.

@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;
}

问题是用户是否按下http://localhost:8080/entities/entity/12并发送POST请求而不是GET. API应该抛出405,但现在抛出500. 实际堆栈跟踪:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException and MIME media type application/octet-stream was not found

Issue is if user hit the http://localhost:8080/entities/entity/12 and sends a POST request instead of GET. API should throw 405 but it is throwing 500 right now. Actual stack trace:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException and MIME media type application/octet-stream was not found

推荐答案

第一种情况很清楚. 405的意思是不允许使用方法".如果特定的URL无法回复当前方法的请求,则通常由容器或框架抛出.但是,您确实使用了正确的方法,但是使用了空的主体,这会导致代码中出现NPE.在这种情况下,预期状态为500.

The first case is clear. 405 means "Method Not Allowed". It is typically thrown by container or framework if specific URL cannot reply to request of current method. But you do use correct method but with empty body that causes NPE in your code. In this case 500 status is expected.

问题2不清楚.您应该发送更多详细信息.您要么不发送POST,要么URL可以接受POST.请仔细检查并发送有关您的配置,一些代码段等的详细信息.

Issue 2 is not clear. You should send more details. You either do not send POST or the URL can accept POST. Please double check and send details about your configuration, some code snippets etc.

发布一些代码后,我可以说GET请求不能消耗任何东西.从findMyEntityById()中删除@Consumes({MediaType.APPLICATION_JSON}).试试吧.如果有帮助,您可以说这是Jersey中的一种错误:实际上,它应该在部署过程中引发GET无法消耗任何东西的异常,或者在处理GET方法时忽略@Consume.

after posting some code I can say that GET request cannot consume anything. Remove @Consumes({ MediaType.APPLICATION_JSON }) from 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.

这篇关于Restful Services:发送HTTP 405而不是HTTP 500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆