我使用Jersey实现创建了Java Rest服务。 我的要求是在我的Rest服务中抛出UserDefined Exception 我创建了Exception类和ExceptionMapper类
这是我的Java代码:
public class AppServerException extends Exception {
private String errorMessage;
private int errorId;
private static final long serialVersionUID = 1L;
// Parameterless Constructor
public IT360AppServerException() {
}
// Constructor that accepts a message
public IT360AppServerException(String message, int errorid) {
super(message);
errorMessage = message;
errorId = errorid;
}
public String getErrorMessage() {
return errorMessage;
}
public int getErrorId() {
return errorId;
}
}
ExceptionMapper类:
@Provider
public class AppServerExceptionMapper implements
ExceptionMapper<AppServerException> {
@Override
public Response toResponse(AppServerException exception) {
System.out.println("toResponse>>");
System.out.println("Error Code >>>>"+exception.getErrorId());////printing
System.out.println("Error Message >>>"+exception.getErrorMessage());//printing
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(exception.getErrorMessage());
errorResponse.setErrorId(exception.getErrorId());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(errorResponse).type(MediaType.APPLICATION_JSON).build();
}
}
问题:
我可以在toResponse()
方法中打印日志,但响应没有返回.Flow正在停止日志本身。可以任何帮助
RestService:
@POST
@Path("/update")
@Consumes("application/json")
@Produces("application/json")
public Response updateIT30(String json,
@HeaderParam("authorization") String authString)
throws UserAuthenticationException, IT360AppServerException {
System.out.println(" JSON Format" + json);
try{
response = client.execute(post);
}
catch(ConnectException e){
throw new IT360AppServerException("App Server is down", 403);
}
}