I have an application and I want it to accept both XML and JSON, how can I program the return type? For example, this is my POJO
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class KeyProvision {
private String Consumer ;
private String API ;
private String AllowedNames ;
public void setConsumer( String Consumer)
{
this.Consumer= Consumer;
}
public void setAPI( String API){
this.API = API;
}
public void setAllowedNames(String AllowedNames){
this.AllowedNames = AllowedNames;
}
@XmlElement(name="Consumer")
public String getConsumer(){
return Consumer;
}
@XmlElement(name="API")
public String getAPI(){
return API;
}
@XmlElement(name="AllowedNames")
public String getAllowedNames(){
return AllowedNames;
}
}
My leisure interface
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@POST
@Path("/request")
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Response getRequest(KeyProvision keyInfo){
String result = "Track saved : " + keyInfo;
return Response.status(201).entity(result).build() ;
}
my xml
<?xml version="1.0" encoding="UTF-8"?>
<KeyProvision>
<Consumer> testConsumer </Consumer>
<API>posting</API>
<AllowedNames> google</AllowedNames>
</KeyProvision>
my json
{
"KeyProvision": {
"Consumer": "testConsumer",
"API": "posting",
"AllowedNames": "google",
}
}
My problems / questions
1) I keep getting 415 error when I use JSON, why is it not canceled? 2) Is the type of repeat defined by JAXB?
source
share