使用Rest CXF管理异常

时间:2015-06-20 16:55:59

标签: java spring web-services 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());
}
}

我想知道如何管理客户端的异常......

对于xample,当我调用此服务并获取并出错时,从后端我已经

   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'

并在客户i中:

 [INFO] Starting scanner at interval of 3 seconds.
 [ERROR] /DisConnectionView/register
 javax.ws.rs.WebApplicationException 

然后      连接被拒绝。

我知道这是一个菜鸟问题,但是一点帮助会很棒......

也许我可以将异常包装在我可以使用服务返回的响应元素中,但是如果我想返回一个简单的UserTO,是否有另一种方法来管理异常并在客户端中获取类型或消息?

谢谢!

1 个答案:

答案 0 :(得分:1)

我建议您使用以下示例。

服务器端# generate data from two different multivariate normal distributions m1 <- c(0,1) sd1 <- matrix(c(1,0.7,.7,1),2,2) m2 <- c(1,0) sd2 <- matrix(c(2,.1,.1,1),2,2) set.seed(2) y1 <- mvrnorm(50,m1,sd1) y2 <- mvrnorm(50,m2,sd2) # this creates data with a single change point y <- rbind(y1,y2) # now use makeDepmix to create a depmix model for this bivariate normal timeseries rModels <- list() rModels[[1]] <- list(MVNresponse(y~1)) rModels[[2]] <- list(MVNresponse(y~1)) trstart=c(0.9,0.1,0.1,0.9) transition <- list() transition[[1]] <- transInit(~1,nstates=2,data=data.frame(1),pstart=c(trstart[1:2])) transition[[2]] <- transInit(~1,nstates=2,data=data.frame(1),pstart=c(trstart[3:4])) instart=runif(2) inMod <- transInit(~1,ns=2,ps=instart,data=data.frame(1)) mod <- makeDepmix(response=rModels,transition=transition,prior=inMod) fm2 <- fit(mod,emc=em.control(random=FALSE)) The output gives this: > summary(fm2) Initial state probabilties model St1 St2 (Intercept) 0 -10.036 Transition matrix toS1 toS2 fromS1 0.98 0.02 fromS2 0.00 1.00 Response parameters Re1.coefficients1 Re1.coefficients2 Re1.Sigma1 Re1.Sigma2 Re1.Sigma3 St1 0.125 1.024 1.346 0.873 1.272 St2 0.716 0.100 2.068 0.285 0.888

javax.ws.rs.ext.ExceptionMapper

服务器配置XML

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();
    }

}

客户端<jaxrs:server id="" address=""> <jaxrs:serviceBeans> .... </jaxrs:serviceBeans> <jaxrs:providers> <bean class="package.RuntimeExceptionRestMapper" /> </jaxrs:providers> </jaxrs:server>

org.apache.cxf.jaxrs.client.ResponseExceptionMapper

客户端配置XML

public class RestResponseExceptionMapper implements ResponseExceptionMapper<Exception> {

    public Exception fromResponse(Response r) {
        //throw you exception
        return new WebApplicationException(r.getStatus());
    }

}