I have an api which is working fine when it comes to XML post request. the XML is recieved in the Post body and processed accordingly. Here is the header of method
@POST
@Path ("{"+num+"}/"+STATUS+"."+XML)
@Consumes (MediaType.APPLICATION_XML)
@Produces (MediaType.APPLICATION_XML)
public Response getStatusXML (@PathParam(num) String num,
JAXBElement<OrderStatusRequestType> jaxbOrderStatusRequestType,
@Context UriInfo requestUriInfo,
@Context SecurityContext securityContext){
OrderStatusRequestType orderStatusRequestType = jaxbOrderStatusRequestType.getValue();
return processRequest (num, XML, orderStatusRequestType, securityContext);
}
This WS is called with this POST body
<orderStatusRequest>
<vendor>32658</vendor>
<key>232X1</key>
</orderStatusRequest>
I have XML schema for that and this request is working fine as per requirement. I get the jaxbObject and i get the orderStatusRequest as well. However, When i try to call my json webservice, I get null orderStatusRequest
. I have a separate method to consume json.
@POST
@Path ("{"+num+"}/"+STATUS+"."+JSON)
@Consumes (MediaType.APPLICATION_JSON)
@Produces (MediaType.APPLICATION_JSON)
public Response getStatusJSON(@PathParam(num) String num,
JAXBElement<OrderStatusRequestType> jaxbOrderStatusRequestType,
@Context UriInfo requestUriInfo,
@Context SecurityContext securityContext){
OrderStatusRequestType orderStatusRequestType= jaxbOrderStatusRequestType.getValue();
return processRequest (num, JSON, orderStatusRequestType, securityContext);
}
I am sending following json in post request
{"orderStatusRequest":{"vendor":"32658","key":"232X1"}}
I get null object in my jaxbElement .. I am unable to understand why. For convenience, here is my XML Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="orderStatusRequestType">
<xs:sequence>
<xs:element name="vendor" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="key" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="orderStatusRequest" type="orderStatusRequestType"/>
</xs:schema>
I wrote a client and send the JSON request
ClientResponse clientResponse = service.path("api")
.path("v1")
.path("personal")
.path("orders")
.path(num)
.path("status.json").accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, jaxbOst);
And it gave me error status 415 i.e content type isn't supported. However, with only 2 modification i.e. status.xml
and MediaType.APPLICATION_XML
works fine.
Update 2:
With Fiddler, I am able to hit the webservice with JSON. But Jersey isn't marchling the input POST body into specific objects. I am getting null in vendor
and key
fields.
UPDATED 3
I am able to consume the JSON from Fiddler. The JSON is {"vendor":"32658","key":"232X1"}
. I was wrapping it under another object earlier. BUT I am still unable to send a request using Jersey Client. What i am guessing is that my JAXB Object is giving XML
when i try to post. How can i make sure that when I use objectFactory.createOderRequestStatus
, it will be translated into JSON and not XML ?