Reputation: 57
i'm new with web services. I'm trying to send an InputStream from an android application to a server that use Jersey 2.22 like Rest framework, but i get a 415 Unsupported media type error. This is my client code( I use AsyncHttpClient of Loopj):
client.addHeader("Content-Type", "application/octet-stream");
final RequestParams requestParams = new RequestParams();
requestParams.put("file", myInputStream);
client.post("MyUrl", requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println("Success");
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
if(responseBody != null)
System.out.println(new String(responseBody, StandardCharsets.UTF_8));
System.out.println(error.getMessage());
System.out.println("Failure");
}
});
Server side code is:
@Path("/schedule")
public class schedule {
@POST
@Secured
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response getScenario(@FormDataParam("file")InputStream payload){
System.out.println("I'm in");
//Do stuff with payload
return Response.ok().build();
}
But if instead of @FormDataParam i only put
public Response getScenario(InputStream payload)
I receive my inputStream without any 415 error(naturally in this case the inputStream contains httpBody too).
Searching on the web i have read about dependencies problem, but all seems ok. Here my pom.xml and Resource Configuration:
final ResourceConfig rc = new ResourceConfig().packages("MyPackages", "org.glassfish.jersey.examples.multipart");
rc.register(MultiPartFeature.class);
Pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<!-- uncomment this to get JSON support: -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9.6</version>
</dependency>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Upvotes: 0
Views: 940
Reputation: 57
I solved simply changing the @Consumes annotation from @Consumes(MediaType.APPLICATION_OCTET_STREAM) to @Consumes(MediaType.MULTIPART_FORM_DATA)
and removing header from client http request
Upvotes: 0