泽西岛客户端收到了错误的回复

时间:2015-02-17 19:48:32

标签: java jersey jersey-2.0 jersey-client

不确定发生了什么,但我对我的回应被劫持的地方感到茫然。

我正在使用jersey 2.15并且有一个自定义异常映射器工作了一段时间。我已经移动了一些代码,现在我的异常映射器没有按预期工作,但问题似乎是灰熊劫持了我的响应。

这是我的问题的关键,如果你在我的日志过滤器中看到服务器正确处理我的异常并告诉我它正在发送带有application / xml的响应。但是,当我从客户端打印响应作为字符串时,不是预期的错误消息,而是来自Web服务器的通用400错误。

Feb 17, 2015 1:37:20 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Server responded with a response on thread Grizzly-worker(2)
2 < 400
2 < Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><constraintViolationErrorResponse><responseCode>2</responseCode><violations><path>createCustomer.arg0.customerContact</path><message>customerContact is a required field</message></violations><violations><path>createCustomer.arg0.technicalContact</path><message>technicalContact is a required field</message></violations><violations><path>createCustomer.arg0.spCustomerID1</path><message>spCustomerID1 is a required field</message></violations><violations><path>createCustomer.arg0.customerAddress</path><message>customerAddress is a required field</message></violations><violations><path>createCustomer.arg0.customerName</path><message>customerName is a required field</message></violations></constraintViolationErrorResponse>

Response is <html><head><title>Grizzly 2.3.16</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Bad Request</div><div class="body">Bad Request</div><div class="footer">Grizzly 2.3.16</div></body></html>
Media type is text/html; charset=ISO-8859-1
Feb 17, 2015 1:37:20 PM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
INFO: Stopped listener bound to [localhost:9998]

这是我的异常映射器,它处理ConstraintViolationException

@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

    //private static final Logger logger = Logger.getLogger(ConstraintViolationExceptionMapper.class.getName());

    public Response toResponse(ConstraintViolationException exception) {
        final int violationCount = exception.getConstraintViolations().size();

        ConstraintViolation<?>[] constraintViolations = exception.getConstraintViolations().toArray(
                new ConstraintViolation<?>[violationCount]);
        Violation[] violations = new Violation[exception.getConstraintViolations().size()];

        for (int i = 0; i < violationCount; i++) {
            ConstraintViolation<?> cv = constraintViolations[i];
            Violation violation = new Violation(cv.getPropertyPath().toString(), cv.getMessage());
            violations[i] = violation;
        }

        ConstraintViolationErrorResponse responseEntity = new ConstraintViolationErrorResponse();
        responseEntity.setViolations(violations);

        return Response.status(Response.Status.BAD_REQUEST).entity(responseEntity).build();
    }
}

这是我正在使用的单元测试,它打印出来自客户端的响应以进行调试。

    @Test
    public void testConstraintViolationException() {
        logger.info("testConstraintViolationException");
        Customer customer = new Customer();
        Entity<Customer> customerEntity = Entity.entity(customer, MediaType.APPLICATION_XML);


        Response response = target("customer").request().accept(MediaType.APPLICATION_XML).post(customerEntity, Response.class);
        if (response.getStatusInfo() != Status.OK) {

            final String res = response.readEntity(new GenericType<String>() {});
            System.out.println("Response is " + res);

            System.out.println("Media type is " + response.getMediaType());
            ConstraintViolationErrorResponse responseEntity = response.readEntity(ConstraintViolationErrorResponse.class);
            assert (responseEntity.getClass() == ConstraintViolationErrorResponse.class);
            ConstraintViolationErrorResponse constraintResponse = (ConstraintViolationErrorResponse) responseEntity;
            assert (constraintResponse.getViolations().length > 0);

            for (Violation v : constraintResponse.getViolations()) {
                logger.fine("Constraint Violation  Path -> " + v.getPath() + " Message -> " + v.getMessage());
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案here

问题是资源配置中的配置问题。