users@jersey.java.net

[Jersey] JSON POST Request not processed

From: markpope <markpope_at_gmail.com>
Date: Fri, 12 Aug 2011 09:13:59 -0700 (PDT)

I have exposed a REST @POST method annotated to produce and consume XML and
JSON. The method parameter and response class is a JAXB object. When called
with wget and an XML request the correct XML responsed is returned. When I
make a wget XML request and ask for a JSON response using the 'accept' http
header I get the correct JSON response returned. My problem is when I use
wget to make a JSON request I get an incorrect response. The JSON request is
not being mapped to the method JAXB object parameter.

I am using Glassfish 3.0.1 and Netbeans 6.9.1 to generate and publish my
war. I enabled logging using the LoggingFilter and can see my JSON request
properly being recieved:
[#|2011-08-11T17:15:12.171-0700|INFO|glassfish3.0.1|com.sun.jersey.api.container.filter.LoggingFilter|_ThreadID=25;_ThreadName=Thread-1;|3
* Server in-bound request
3 > POST
http://localhost:38080/RetrieveInventoryService/resources/_2010_04_22/retrieveinventoryservice
3 > user-agent: Wget/1.12 (linux-gnu)
3 > accept: application/json
3 > host: localhost:38080
3 > connection: Keep-Alive
3 > content-type: application/json
3 > content-length: 242
3 >
{"inventorydata": {
     "messageheader":{
         "timedate":"2011-08-09T21:12:10.092Z",
         "source":"smf1-idweb-01.mercury.com",
         "client":"10395",
         "guid":"7cb18cc0-f945-47c6-9a8f-789bbc345a51"
     }
  }
}



I have added a custom JAXB Context Resolver(using natural builder) and see
my logger messages when it is called with each JSON or XML request. My
method is called with an empty JAXB object. The object when marshalled
prints an empty XML payload:
[#|2011-08-11T17:16:40.887-0700|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=24;_ThreadName=Thread-1;|<?xml
version="1.0" encoding="UTF-8" standalone="yes"?><inventoryData
xmlns="http://www.ragingwire.com/soa/2010-04-22/InventoryData.xsd"/>|#]



This is my REST annotated class:
package com.ragingwire.soa._2010_04_22.inventorydata;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

@Path("/_2010_04_22/retrieveinventoryservice")
public class RetrieveInventoryData {
    @Context private UriInfo context;

    
    public RetrieveInventoryData() {
    }

    
    @POST
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public InventoryData retrieveInventoryServiceOperation(InventoryData
pRequest) {
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(InventoryData.class);
            Marshaller marshall = context.createMarshaller();
             marshall.marshal(pRequest, System.out);
        } catch (JAXBException ex) {
           
Logger.getLogger(RetrieveInventoryData.class.getName()).log(Level.SEVERE,
null, ex);
        }


        MessageHeader header = new MessageHeader();
        header.setClient("myClient");
        header.setSource("my localhost");
        pRequest.setMessageHeader(header);
        return pRequest;
    }
}



This is the XML Root Element JAXB generated class:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB)
Reference Implementation, vhudson-jaxb-ri-2.2-147
// See http://java.sun.com/xml/jaxb http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the
source schema.
// Generated on: 2011.08.11 at 05:05:43 PM PDT
//


package com.ragingwire.soa._2010_04_22.inventorydata;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 *
 * <p>The following schema fragment specifies the expected content contained
within this class.
 *
 * <pre>
 * &lt;complexType>
 * &lt;complexContent>
 * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 * &lt;sequence>
 * &lt;element name="messageHeader"
type="{http://www.ragingwire.com/soa/2010-04-22/InventoryData.xsd}MessageHeader"/>
 * &lt;element name="messagePayload"
type="{http://www.ragingwire.com/soa/2010-04-22/InventoryData.xsd}MessagePayload"/>
 * &lt;/sequence>
 * &lt;/restriction>
 * &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "messageHeader",
    "messagePayload"
})
@XmlRootElement(name = "inventoryData")
public class InventoryData {

    @XmlElement(required = true)
    protected MessageHeader messageHeader;
    @XmlElement(required = true)
    protected MessagePayload messagePayload;

    /**
     * Gets the value of the messageHeader property.
     *
     * @return
     * possible object is
     * {_at_link MessageHeader }
     *
     */
    public MessageHeader getMessageHeader() {
        return messageHeader;
    }

    /**
     * Sets the value of the messageHeader property.
     *
     * @param value
     * allowed object is
     * {_at_link MessageHeader }
     *
     */
    public void setMessageHeader(MessageHeader value) {
        this.messageHeader = value;
    }

    /**
     * Gets the value of the messagePayload property.
     *
     * @return
     * possible object is
     * {_at_link MessagePayload }
     *
     */
    public MessagePayload getMessagePayload() {
        return messagePayload;
    }

    /**
     * Sets the value of the messagePayload property.
     *
     * @param value
     * allowed object is
     * {_at_link MessagePayload }
     *
     */
    public void setMessagePayload(MessagePayload value) {
        this.messagePayload = value;
    }

}


This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
       
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
         <init-param>
          
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
          
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
         </init-param>
         <init-param>
          
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
           <param-value>true</param-value>
         </init-param>
     <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>



My wget XML call requesting JSON:

wget --header="Accept: application/json" --header="Content-Type:
application/xml" --post-data "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"yes\"?><inventoryData
xmlns=\"http://www.ragingwire.com/soa/2010-04-22/InventoryData.xsd\"><messageHeader><client>10395</client></messageHeader></inventoryData>"
http://localhost:38080/RetrieveInventoryService/resources/_2010_04_22/retrieveinventoryservice


My wget JSON call requesting JSON:

wget --header="Accept: application/json" --header="Content-Type:
application/json" --post-data "{\"inventorydata\": { \"messageheader\":{
\"timedate\":\"2011-08-09T21:12:10.092Z\",
\"source\":\"smf1-idweb-01.mercury.com\", \"client\":\"10395\",
\"guid\":\"7cb18cc0-f945-47c6-9a8f-789bbc345a51\" } } }"
http://localhost:38080/RetrieveInventoryService/resources/_2010_04_22/retrieveinventoryservice



--
View this message in context: http://jersey.576304.n2.nabble.com/JSON-POST-Request-not-processed-tp6680972p6680972.html
Sent from the Jersey mailing list archive at Nabble.com.