使用 Rest CXF 管理异常
Manage Exception with Rest CXF
我接触休息服务的时间不多了,我想问一个简单的问题......至少我希望它很简单!
我有这个服务:
@Override
public UserTO register(UserTO userTO) {
UserTO user=null;
try{
user=presentationService.register(userTO);
}
catch(ConstraintViolationException ex)
{
throw new CustomException(ex.getMessage());
}
return user;
}
我有这个客户:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm")UserTO user, Map<String, Object> model) {
UserTO userTO=null;
String error="";
Response resp;
userTO=registerme(client, user);
}
这是个例外:
public class CustomException extends WebApplicationException {
public CustomException(String message) {
super(Response.status(Response.Status.BAD_REQUEST)
.entity(message).type(MediaType.TEXT_PLAIN).build());
}
}
我想知道如何管理来自客户端的异常...
对于示例,当我调用此服务并收到错误时,从后端我已经:
2015-06-20 18:44:28 WARN SqlExceptionHelper:143 - SQL Error: 1062, SQLState: 23000
2015-06-20 18:44:28 ERROR SqlExceptionHelper:144 - Duplicate entry 'aa' for key 'login_UNIQUE'
2015-06-20 18:44:28 INFO LoggingOutInterceptor:233 - Outbound Message
---------------------------
ID: 1
Response-Code: 400
Content-Type: text/plain
Headers: {Content-Type=[text/plain], Date=[Sat, 20 Jun 2015 16:44:28 GMT]}
Payload: Nn funziona! Duplicate entry 'aa' for key 'login_UNIQUE'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry 'aa' for key 'login_UNIQUE'
我在客户端中:
[INFO] Starting scanner at interval of 3 seconds.
[ERROR] /DisConnectionView/register
javax.ws.rs.WebApplicationException
然后
连接被拒绝。
我知道这是一个菜鸟问题,但如果有一点帮助就更好了..
也许我可以将异常包装在我可以 return 使用该服务的响应元素中,但是如果我想 return 一个简单的 UserTO,是否有另一种方法来管理异常并在客户端中获取类型或消息?
谢谢!
我建议你使用下面的例子。
服务器端javax.ws.rs.ext.ExceptionMapper
public class RuntimeExceptionRestMapper implements ExceptionMapper<RuntimeException> {
public Response toResponse(RuntimeException exception) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
//handle your response
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(exception.getMessage())
.build();
}
}
服务器配置XML
<jaxrs:server id="" address="">
<jaxrs:serviceBeans>
....
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="package.RuntimeExceptionRestMapper" />
</jaxrs:providers>
</jaxrs:server>
客户端org.apache.cxf.jaxrs.client.ResponseExceptionMapper
public class RestResponseExceptionMapper implements ResponseExceptionMapper<Exception> {
public Exception fromResponse(Response r) {
//throw you exception
return new WebApplicationException(r.getStatus());
}
}
客户端配置XML
<jaxrs:client id="service" address="" serviceClass="">
<jaxrs:providers>
<bean class="package.RestResponseExceptionMapper" />
</jaxrs:providers>
</jaxrs:client>
我接触休息服务的时间不多了,我想问一个简单的问题......至少我希望它很简单!
我有这个服务:
@Override
public UserTO register(UserTO userTO) {
UserTO user=null;
try{
user=presentationService.register(userTO);
}
catch(ConstraintViolationException ex)
{
throw new CustomException(ex.getMessage());
}
return user;
}
我有这个客户:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm")UserTO user, Map<String, Object> model) {
UserTO userTO=null;
String error="";
Response resp;
userTO=registerme(client, user);
}
这是个例外:
public class CustomException extends WebApplicationException {
public CustomException(String message) {
super(Response.status(Response.Status.BAD_REQUEST)
.entity(message).type(MediaType.TEXT_PLAIN).build());
}
}
我想知道如何管理来自客户端的异常...
对于示例,当我调用此服务并收到错误时,从后端我已经:
2015-06-20 18:44:28 WARN SqlExceptionHelper:143 - SQL Error: 1062, SQLState: 23000
2015-06-20 18:44:28 ERROR SqlExceptionHelper:144 - Duplicate entry 'aa' for key 'login_UNIQUE'
2015-06-20 18:44:28 INFO LoggingOutInterceptor:233 - Outbound Message
---------------------------
ID: 1
Response-Code: 400
Content-Type: text/plain
Headers: {Content-Type=[text/plain], Date=[Sat, 20 Jun 2015 16:44:28 GMT]}
Payload: Nn funziona! Duplicate entry 'aa' for key 'login_UNIQUE'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry 'aa' for key 'login_UNIQUE'
我在客户端中:
[INFO] Starting scanner at interval of 3 seconds.
[ERROR] /DisConnectionView/register
javax.ws.rs.WebApplicationException
然后 连接被拒绝。
我知道这是一个菜鸟问题,但如果有一点帮助就更好了..
也许我可以将异常包装在我可以 return 使用该服务的响应元素中,但是如果我想 return 一个简单的 UserTO,是否有另一种方法来管理异常并在客户端中获取类型或消息?
谢谢!
我建议你使用下面的例子。
服务器端javax.ws.rs.ext.ExceptionMapper
public class RuntimeExceptionRestMapper implements ExceptionMapper<RuntimeException> {
public Response toResponse(RuntimeException exception) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
//handle your response
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(exception.getMessage())
.build();
}
}
服务器配置XML
<jaxrs:server id="" address="">
<jaxrs:serviceBeans>
....
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="package.RuntimeExceptionRestMapper" />
</jaxrs:providers>
</jaxrs:server>
客户端org.apache.cxf.jaxrs.client.ResponseExceptionMapper
public class RestResponseExceptionMapper implements ResponseExceptionMapper<Exception> {
public Exception fromResponse(Response r) {
//throw you exception
return new WebApplicationException(r.getStatus());
}
}
客户端配置XML
<jaxrs:client id="service" address="" serviceClass="">
<jaxrs:providers>
<bean class="package.RestResponseExceptionMapper" />
</jaxrs:providers>
</jaxrs:client>