Reputation: 683
I have a rest call which is taking some parameters as FormDataParam. When I am passing object EngineConfigMeta in json string to the rest call from postman, at the restcall level the object is not getting deserialized properly.
Rest-call
@Path( "/add-config" )
@POST
@Consumes( MediaType.MULTIPART_FORM_DATA )
@Produces( MediaType.APPLICATION_JSON )
public Response addConfig( @FormDataParam( "config" ) EngineConfigMeta config,
@FormDataParam( "file" ) InputStream configFileInputStream,
@FormDataParam( "file" ) FormDataContentDisposition cdh)
{
return Response.ok(Response.Status.OK).entity(buildJson(config.getVersion())).build();
}
EngineConfigMeta.java
public class EngineConfigMeta {
private String tenantName;
private long version;
EngineConfigMeta(String tenantName, long version) {
this.tenantName = tenantName;
this.version = version;
}
..getters and setters
}
This is how I am passing the parameters to rest call using postman - Postman screenshot
Now the problem is when I debug the code of rest call, I am getting all the json string assigned to only one property on EngineConfigMeta pojo -
EngineConfigMeta{tenantName={"tenantName": "abc", "version": 2}, version=0}
As you can see above that the whole object json string is assigned to tenantName property. So deserialization is not happening correctly here.
Please help me.
Upvotes: 3
Views: 4215
Reputation: 209004
It's because the client needs to set the Content-Type
header for the individual "config"
part. If you don't do this, then it will default to text/plain
. Because you have a constructor that accepts the String, Jersey just assumes to assign the value of the constructor argument to the incoming part data.
In Postman I don't think you can set the individual part's Content-Type. What you need to do is manually set the type on the server side using a FormDataBodyPart
. Then you can manually get the EngineConfigMeta
.
public Response post(@FormDataParam("config") FormDataBodyPart part) {
part.setMediaType(MediaType.APPLICATION_JSON_TYPE);
EngineConfigMeta meta = part.getValueAs(EngineConfigMeta.class);
}
See also:
Upvotes: 3