2016-06-09 39 views
-1

Jersey的默認異常處理是什麼(當沒有提供ExceptionMapper時)?Jersey的默認異常處理是什麼? (如果沒有提供ExceptionMapper)

例子:

@GET 
@Path("/rest") 
public String rest() { 
    throw new RuntimeException("Wonder what would happen..."); 
} 

會是什麼結果呢? HTTP狀態和內容會返回什麼?

+1

爲什麼要問時,你可以試試嗎? – zapl

+1

這就是所謂的知識共享。提問也很重要。給別人回答的機會... –

回答

0

您的函數必須返回一個Response對象(javax.ws.rs.core.Response)。

@GET 
@Path("/rest") 
public Response invokeSomething() { 
    return Response.ok("Some string").build(); 
} 

該方法可以返回202 resoponse與註釋類中序列化的對象。 Es:

@Consumes(MediaType.APPLICATION_JSON) 
@Produces({MediaType.APPLICATION_JSON}) 
public class .... 

這個註解表示我的服務使用了一個json,而我的響應是一個json。

然後,當你想要回你必須返回一個代碼500 在您爲例錯誤:

@GET 
    @Path("/rest") 
    public Response invokeSomething() { 
     return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("exeption message").build(); 
    } 
相關問題