我尝试上传图片并使用带衬衫的RestService。如果我测试我的休息服务它将完美地工作,但如果调试测试我得到这些错误消息。有人有什么想法吗?
堆栈追踪:
testCreateImage(com.myrest.api.rest.v1.LoyalitiyResourceTest) Time elapsed: 1.829 sec <<< ERROR!
com.sun.jersey.api.client.ClientHandlerException: java.io.IOException: Error writing request body to serverat sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3191)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3174)
at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:90)
at com.sun.jersey.core.util.ReaderWriter.writeTo(ReaderWriter.java:115)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeTo(AbstractMessageReaderWriterProvider.java:76)
at com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FileProvider.java:103)
at com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FileProvider.java:64)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:213)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.filter.HTTPBasicAuthFilter.handle(HTTPBasicAuthFilter.java:81)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.put(WebResource.java:537)
at com.myrest.api.rest.v1.LoyalitiyResourceTest.testCreateImage(LoyalitiyResourceTest.java:318)
jerey注释课程。
@PUT
@RolesAllowed({"Registered", "Author", "Editor", "Publisher", "Manager", "Administrator", "Super Users"})
@Path("/{id}/image/{type}")
@Consumes("image/*")
public Response setImage(@Context SecurityContext context, @Context UriInfo uriInfo,
@PathParam("id") Integer id, @PathParam("type") String type, InputStream imageStream) {
final String mn = "setCardImage";
LOGGER.entering(SOURCE_CLASS, mn);
try {
getService().setCardImage(getCurrentUser(context.getUserPrincipal()), id, type, imageStream);
}
catch (Exception e) {
LOGGER.throwing(SOURCE_CLASS, mn, e);
WebApplicationException ex = new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
throw ex;
}
LOGGER.exiting(SOURCE_CLASS, mn);
return Response.created(uriInfo.getRequestUri()).build();
}
JUnit Test在这一行
ClientResponse response = wr.put(ClientResponse.class,testLENA);
错误。
@Test
public void testCreateImage() {
//create Image
final String url ="/loyality/4/image/front";
WebResource webResource = resource();
webResource.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
File testLENA = new File("src/test/resources/images/lena.png");
Builder wr = webResource.path(url).header("open-api", "test").type("image/png");
ClientResponse response = wr.put(ClientResponse.class, testLENA);
Assert.assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
Assert.assertNotNull(response.getEntity(String.class));
}