【问题标题】:HTTP 400 Bad Request : javax.ws.rs.BadRequestExceptionHTTP 400 错误请求:javax.ws.rs.BadRequestException
【发布时间】:2015-08-21 18:24:03
【问题描述】:

我创建了一个 RESTful Web 服务并编写了一个客户端来使用它。但是当我运行它时,我接受 HTTP 400 Bad Request : javax.ws.rs.BadRequestException exeption 。这是我的客户代码:

    String webserviceURI = "http://localhost:8084/fsc-access";

ClientConfig clientConfig = new ClientConfig();
Client client = ClientBuilder.newClient(clientConfig);
URI serviceURI = UriBuilder.fromUri(webserviceURI).build();
WebTarget webTarget = client.target(serviceURI);
MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("plate", plate);
formData.add("startTime", start.toString());
formData.add("endTime", end.toString());
Weightings weightings = new Weightings();
 weightings.getWeightings().addAll((Collection<? extends Weighting>) webTarget.path("rest").path("report").path("loadWeightingByPlate").
        request().accept(MediaType.APPLICATION_XML).post(javax.ws.rs.client.Entity.form(formData), Weightings.class));

这是我的网络服务:

@Path("/report")
public class WeightingRESTfulService {

@POST
@Path("/loadWeightingByPlate")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Weightings LoadWeightingInSpecTimeInSpecPlate(
        @FormParam("plate") String plate,
        @FormParam("startTime") String _startTime,
        @FormParam("endTime") String _endTime,
        @Context HttpServletRequest req) {
    Long startTime = new Long(_startTime);
    Long endTime = new Long(_endTime);
    try {
        Weightings weightings = new Weightings();
        weightings.getWeightings().addAll(Weighting.LoadWeightingInSpecTimeInSpecPlate(startTime, endTime, plate));
        System.out.println("no error");
        return weightings;
    } catch (Exception ex) {
        System.out.println("Exception = " + ex);
        return null;
    }
}
}

谁能帮我使用这个网络服务?

有一些警告:

21-Aug-2015 23:18:11.797 警告 [http-nio-8084-exec-123] org.glassfish.jersey.servlet.WebComponent.filterFormParameters 对 URI http://localhost:8084/fsc-access/rest/report/loadWeightingByPlate 的 servlet 请求包含表单参数请求正文,但请求正文已被 servlet 或访问请求参数的 servlet 过滤器使用。只有使用 @FormParam 的资源方法才能按预期工作。通过其他方式消耗请求正文的资源方法将无法按预期工作。

还有一些经验:

线程“C3P0PooledConnectionPoolManager[identityToken->1hge1379bmmvkmpse6n4w|7936e088]-AdminTaskTimer”中的异常 java.lang.IllegalStateException:无法用 java.lang.IllegalStateException 覆盖原因:非法访问:此 Web 应用程序实例已经停止。无法加载 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask。最终的后续堆栈跟踪是由出于调试目的引发的错误以及试图终止导致非法访问的线程引起的,并且没有功能影响。 在 java.lang.Throwable.initCause(Throwable.java:457) 在 org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1335) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1216) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) 在 com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040) 在 com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507) 在 com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477) 在 com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565) 在 com.mchange.v2.resourcepool.BasicResourcePool.access 1900 美元(BasicResourcePool.java:44) 在 com.mchange.v2.resourcepool.BasicResourcePool$CullTask​​.run(BasicResourcePool.java:2089) 在 java.util.TimerThread.mainLoop(Timer.java:555) 在 java.util.TimerThread.run(Timer.java:505) 引起:java.lang.ClassNotFoundException 在 org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334) ... 10 更多 线程“C3P0PooledConnectionPoolManager[identityToken->1hge1379bmmw228sz1sso|53826b99]-AdminTaskTimer”中的异常 java.lang.IllegalStateException:无法用 java.lang.IllegalStateException 覆盖原因:非法访问:此 Web 应用程序实例已停止。无法加载 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask。最终的后续堆栈跟踪是由出于调试目的引发的错误以及试图终止导致非法访问的线程引起的,并且没有功能影响。 在 java.lang.Throwable.initCause(Throwable.java:457) 在 org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1335) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1216) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) 在 com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040) 在 com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507) 在 com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477) 在 com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565) 在 com.mchange.v2.resourcepool.BasicResourcePool.access 1900 美元(BasicResourcePool.java:44) 在 com.mchange.v2.resourcepool.BasicResourcePool$CullTask​​.run(BasicResourcePool.java:2089) 在 java.util.TimerThread.mainLoop(Timer.java:555) 在 java.util.TimerThread.run(Timer.java:505) 引起:java.lang.ClassNotFoundException 在 org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334) ... 10 更多

日志过滤器:

22-Aug-2015 00:32:32.969 INFO [http-nio-8084-exec-37] org.glassfish.jersey.filter.LoggingFilter.log 1 * Sending client request on thread http-nio-8084-exec-37
1 > POST http://localhost:8084/fsc-access/rest/report/loadWeightingByPlate
1 > Accept: application/xml
1 > Content-Type: application/x-www-form-urlencoded

22-Aug-2015 00:32:33.015 INFO [http-nio-8084-exec-37] org.glassfish.jersey.filter.LoggingFilter.log 2 * Client response received on thread http-nio-8084-exec-37    
2 < 200
    2 < Content-Length: 1026
    2 < Content-Type: application/xml
    2 < Date: Fri, 21 Aug 2015 19:54:48 GMT
    2 < Server: Apache-Coyote/1.1

【问题讨论】:

  • 日志中是否有任何堆栈跟踪?通常“400 Bad Request”表示找到了资源(否则为404),但无法填写资源的参数。
  • 我指的是服务器日志,而不是客户端日志
  • 是的,有一个警告我添加它有问题。这与我在以前的问题中添加的相同。
  • 我从系统中给出了一些输出,我发现我的加载功能正常工作并向 webservice 返回一个列表。但此列表不会返回给我的客户。
  • 您是否介意更改您的客户端,并在Client client = ClientBuilder.newClient(clientConfig); 行之后添加client.addFilter(new LoggingFilter(System.out));。再次尝试运行它,它应该将整个请求打印到 System.out。显然,您的程序化客户端发出的请求与您手动验证服务正确性的请求之间存在差异。

标签: java web-services jax-rs webclient restful-url


【解决方案1】:

您的资源正在返回一个Weightings 的实例,因此您只需要转换它,您不需要执行addAll()

Weightings weightings = new Weightings();
 weightings.getWeightings().addAll((Collection<? extends Weighting>) webTarget.path("rest").path("report").path("loadWeightingByPlate").
        request().accept(MediaType.APPLICATION_XML).post(javax.ws.rs.client.Entity.form(formData), Weightings.class));

应该是:

Weightings weightings = (Weightings) webTarget.path("rest").path("report").path("loadWeightingByPlate").
    request().accept(MediaType.APPLICATION_XML).post(javax.ws.rs.client.Entity.form(formData), Weightings.class));

它不会修复你的 400 异常,但是没有它你会得到一个 ClassCastException。

【讨论】:

  • 但我有 java.lang.InstantiationException
猜你喜欢
  • 2017-07-21
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多