当前位置: 首页 > 知识库问答 >
问题:

泽西客户端收到错误的响应

翟越
2023-03-14

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

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]
@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个答案

袁成化
2023-03-14

我在这里找到了解决办法

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

 类似资料:
  • 当我的客户机应用程序尝试从我的安全RESTWeb服务请求资源时,我得到了粘贴在下面的错误。 我已经在这个主题上找到了这个答案,但是额外的代码似乎很极端。使用泽西岛客户端访问安全的宁静Web服务 有谁知道解决问题的更简单的方法吗? 我的项目:在服务器端,我正在使用jdbc realm的基本身份验证,这在浏览器中使用时工作正常。在客户端,我设置了用户名和密码,当我发送请求时发生错误。如果您需要代码片段

  • 我使用客户机对一个API进行REST调用,该API返回mulipart中的一个Json和一个PDF文件作为响应的第一和第二部分。 使用上面的客户端解析这个多部分响应的正确方法是什么?

  • 我有一个网站,我只是想写一个脚本,发送字符串到一个文本输入在页面(唯一的一个)和点击提交。我构建了一个selenium系统,但被告知这是矫枉过正的,访问页面上的endpoint并以这种方式发送文本要“容易”得多。 线程“main”java.lang.IllegalStateException中出现异常:未找到InjectionManagerFactory。在org.glassfish.jersey

  • 我设计了一个多部分Jersey REST服务,如下所示,用于接收多部分请求(文件上传)并将文件保存在磁盘位置: 我的测试客户是: 但我无法得到回应。 当我使用HTML网页作为客户端通过调用REST服务上传文件时,它工作得很好,但从REST客户端它不工作。 客户有什么需要更改的吗?

  • 问题内容: 我正在尝试使用Jersey客户端模拟对我的Web服务的HTTP请求。我尝试实现文档中的简单示例。这是我的短代码: 我什至没有实现整个示例,因为当前我在最后一行收到一个异常: 我只将此依赖项添加到我的: 我试图用谷歌搜索问题,以及调试应用程序,但我真的看不出问题出在哪里。 编辑 所有Maven依赖项: 问题答案: 这看起来像与JAX-RS API版本(包含MultiValuedMap)有

  • 我使用这个代码来调用泽西客户端的泽西JAX-RS服务。 当响应状态代码为 200 时,此代码工作正常。但对于 200 以外的任何内容,此代码会引发异常。 如何修改此代码,以便根据响应的状态代码执行某些操作?