java HTTP 400 错误请求:javax.ws.rs.BadRequestException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32147028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:43:59  来源:igfitidea点击:

HTTP 400 Bad Request : javax.ws.rs.BadRequestException

javaweb-servicesjax-rswebclientrestful-url

提问by Fatemeh Moh

I create a RESTful web service and write a client to use it . but when I run it i take HTTP 400 Bad Request : javax.ws.rs.BadRequestException exeption . this is my client code :

我创建了一个 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));

and this is my web Service :

这是我的网络服务:

@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;
    }
}
}

can any one help me to use this web Service ?

有人可以帮我使用这个网络服务吗?

there is some warning :

有一些警告:

21-Aug-2015 23:18:11.797 WARNING [http-nio-8084-exec-123] org.glassfish.jersey.servlet.WebComponent.filterFormParameters A servlet request to the URI http://localhost:8084/fsc-access/rest/report/loadWeightingByPlatecontains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

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

and there is som exeprions :

并且有一些 exeprions :

Exception in thread "C3P0PooledConnectionPoolManager[identityToken->1hge1379bmmvkmpse6n4w|7936e088]-AdminTaskTimer" java.lang.IllegalStateException: Can't overwrite cause with java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. at java.lang.Throwable.initCause(Throwable.java:457) at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1335) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1216) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040) at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507) at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477) at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565) at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:44) at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:2089) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505) Caused by: java.lang.ClassNotFoundException at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334) ... 10 more Exception in thread "C3P0PooledConnectionPoolManager[identityToken->1hge1379bmmw228sz1sso|53826b99]-AdminTaskTimer" java.lang.IllegalStateException: Can't overwrite cause with java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. at java.lang.Throwable.initCause(Throwable.java:457) at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1335) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1216) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1040) at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1507) at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1477) at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1565) at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:44) at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:2089) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505) Caused by: java.lang.ClassNotFoundException at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334) ... 10 more

线程“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。WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334) ... 线程“C3P0PooledConnectionPoolManager[identityToken->1hge1379bmmw228sz1sso|53826b99]-AdminTaskTimer”中的另外10个异常:java.lang.IllegalStateException IllegalStateException:访问:此 Web 应用程序实例已停止。无法加载 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask。最终的后续堆栈跟踪是由出于调试目的抛出的错误以及尝试终止导致非法访问的线程引起的,并且没有功能影响。在 org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1335) 在 org.apache 的 java.lang.Throwable.initCause(Throwable.java:457)。

loggingfilter :

日志过滤器:

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

回答by Samuel

Your resource is returning an instance of Weightings, so you just need to cast it, you don't need to do the addAll()

您的资源正在返回 的实例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));

Should be:

应该:

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

It won't fix your 400 exception, but you'll get a ClassCastException without it.

它不会修复您的 400 异常,但如果没有它,您会得到 ClassCastException。