![](/img/trans.png)
[英]restful web services: HTTP Status 500.The server encountered an internal error that prevented it from fulfilling this request error
[英]Restful Services: Sending HTTP 405 instead of HTTP 500 error
有兩個問題:
問題1:
當用戶未為POST api調用之一發送任何請求有效負載時。 其對應的RestController方法,處理傳入的請求並引發空指針異常。 它發回HTTP 500錯誤代碼和堆棧跟蹤作為錯誤描述。
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;
}
問題2:
有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;
}
問題是用戶是否點擊了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
第一種情況很明顯。 405表示“不允許使用方法”。 如果特定的URL無法回復當前方法的請求,則通常由容器或框架拋出。 但是,您確實使用了正確的方法,但是主體為空,導致代碼中出現NPE。 在這種情況下,預期狀態為500。
問題2尚不清楚。 您應該發送更多詳細信息。 您要么不發送POST,要么URL可以接受POST。 請仔細檢查並發送有關您的配置,一些代碼段等的詳細信息。
編輯:
發布一些代碼后,我可以說GET請求不能使用任何東西。 從findMyEntityById()
刪除@Consumes({MediaType.APPLICATION_JSON} findMyEntityById()
。 試試吧。 如果有幫助,您可以說這是Jersey中的一種錯誤:實際上,它應該在部署過程中引發GET無法消耗任何東西的異常,或者在處理GET
方法時忽略@Consume
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.