I have an API which accepts both json and text as request body.
@Path("submitObjects")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public List<APIResult> submitMultiObjects(@DefaultValue("false") @QueryParam(("suppressWarnings"))Boolean suppressWarnings,List<MyObject> objects) {
// API logic
}
class MyObject{
private String name;
private String description;
// and getters and setters
}
When I hit the API with request body in JSON it works fine.
[{
"name": "name1",
"description": "description1"
},{
"name": "name2",
"description": "description2"
}]
success for above request body
But when I pass request body in plain text it fails with error code 415 - Unsupported media type
name=name1
description=description1
failed for above request body
name=name1
description=description1
name=name2
description=description2
failed for above request body
I wanted to know how we can pass list of objects in plain/ text format.
For below API where I am using MyObject instead of List
@Path("submitObject")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public List<APIResult> submitObject(@DefaultValue("false") @QueryParam(("suppressWarnings"))Boolean suppressWarnings,MyObject object) {
// API logic
}
name=name1
description=description1
above request body works fine.