简体   繁体   中英

Unable to consume JSON REST WS

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 ie content type isn't supported. However, with only 2 modification ie 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 ?

The 415 indicates that your client is not sending the correct content type in the request. If you look at your path, for both the XML and the JSON methods, they are identical so you'll need to ensure your client sends the correct content path in order to distinguish which method will service your request. Otherwise Jersey will just pick the first one, which is what is happening here.

There is discussion on this topic in another thread . I haven't tried it myself however can you add some headers to your client to see if that works? Perhaps something like the following:

ClientResponse clientResponse = service.path("api")
                                .path("v1")
                                .path("personal")
                                .path("orders")
                                .path(num)
                                .path("status.json")
                                .accept(MediaType.APPLICATION_JSON)
                                .header("content-type", MediaType.APPLICATION_JSON)
                                .post(ClientResponse.class, jaxbOst);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM