Reputation: 1445
I have the following REST API to parse the given JSON:
POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("/test")
public String getText(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception {
when I test it using the chrome extension postman, the filedetail.getName() is working however the input stream received is null. here the post request I sent :
POST /parse/test HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="1.json"
Content-Type:
----WebKitFormBoundaryE19zNvXGzXaLvS5C
The inputstream received is null .
Note: if I set the content type to "multipart/form-data" I got an exception :
java.lang.NullPointerException
com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:245)
com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:172)
com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:158)
com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:85)
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:490)
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:555)
com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:122)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183)
so I send it without any header, how can I read the file I sent from the postman, is there anything wrong with my REST API ?
Upvotes: 0
Views: 1448
Reputation: 1574
do you use org.glassfish.jersey.bundle
(jaxrs-ri) ?
if you do, you have to add MultiPartFeature.class
to your ApplicationConfigure.java (which contains the Override of getClasses())
if you use grizzly so you have to put and register that class in ResourceConfig
.
here an example for both
first grizzly
public static HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig().packages(true, "your.controllers.package");
rc.register(MultiPartFeature.class);
return GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:8080/"),rc);
}
now the jersey
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
addResources(resources);
return resources;
}
private void addResources(Set<Class<?>> resources) {
resources.add(MultiPartFeature.class);
}
I also remove Consumes
annotation from my method (I believe it detect it as multipart/form-data by default) and also remove content-type from client request because in this case, it cause error 400
Upvotes: 0