我创建了一个RESTful web服务,并编写了一个客户机来使用它。但是当我运行它时,我会使用HTTP400Bad请求: 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));这是我的web服务:
@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;
}
}
}有人能帮我使用这个web服务吗?
有一些警告:
2005年8月21日-201523:18:11.797警告org.glassfish.jersey.servlet.WebComponent.filterFormParameters -nio-8084-EXEC-123 http://localhost:8084/fsc-access/rest/report/loadWeightingByPlate一个servlet对URI http://localhost:8084/fsc-access/rest/report/loadWeightingByPlate的请求包含请求体中的表单参数,但是请求体已经被访问请求参数的servlet或servlet过滤器所消耗。只有使用@FormParam的资源方法才能按预期工作。以其他方式使用请求体的资源方法将无法按预期工作。
还有一些索然无味的出口品:
线程"C3P0PooledConnectionPoolManageridentityToken->1hge1379bmmvkmpse6n4w|7936e088-AdminTaskTimer“java.lang.IllegalStateException中的异常:无法用java.lang.IllegalStateException:非法访问覆盖原因:此web应用程序实例已经停止。无法加载com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.最终的堆栈跟踪是由为调试目的抛出的错误以及试图终止导致非法访问的线程而引起的,没有功能影响。在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(BasicResourcePoolcom.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由: org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334)的java.lang.ClassNotFoundException引起的.run(Timer.java:505) .线程"C3P0PooledConnectionPoolManageridentityToken->1hge1379bmmw228sz1sso|53826b99-AdminTaskTimer“java.lang.IllegalStateException中的另外10种异常:无法用java.lang.IllegalStateException覆盖原因:非法访问:这个web应用程序实例已经停止。无法加载com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.最终的堆栈跟踪是由为调试目的抛出的错误以及试图终止导致非法访问的线程而引起的,没有功能影响。在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(BasicResourcePoolcom.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由: org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1334)的java.lang.ClassNotFoundException引起的.run(Timer.java:505) .又增加了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发布于 2015-08-21 19:29:51
您的资源正在返回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。
https://stackoverflow.com/questions/32147028
复制相似问题