【问题标题】:Jersey client is receiving the wrong responseJersey 客户端收到错误的响应
【发布时间】:2015-02-17 19:48:31
【问题描述】:

不确定发生了什么,但我不知道我的响应在哪里被劫持了。

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

这是我的问题的症结所在,如果您在我的日志过滤器中看到服务器正在正确处理我的异常并告诉我它正在使用 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());
            }
        }
    }

【问题讨论】:

  • 快速说明,如果我返回 ACCEPTED 或 OK 作为响应代码,则响应已成功处理。

标签: java jersey jersey-2.0 jersey-client


【解决方案1】:

我找到了解决方案here

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

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多