Jose Antonio
Jose Antonio

Reputation: 598

JAX-RS - remove the object name (RootElement) in the json of a request POST

I use JAX-RS for RESTful services and when a client (angularjs, soapui, etc) makes a request POST, needs to add the object name (root element) in the json. For example:

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser (UserVo userVo){
    ...
    ...
}

--

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UserVo implements Serializable, Comparable<UserVo>{
    ...
    ...
}

Request POST:

{
    userVo:{
        email:"[email protected]",
        mobilePhone:1234456
    }
}

The clients need to specify "userVo" (if I remove @XmlRootElement from UserVo, not recognize the object), but I don´t want to specify it to improve my REST Api:

{
    email:"[email protected]",
    mobilePhone:1234456
}

If don´t specify the object name, returns an error:

16:04:04,278 WARN [http-bio-8080-exec-3] AbstractJAXBProvider:660 - javax.xml.bind.UnmarshalException - with linked exception: [com.sun.istack.SAXParseException2; columnNumber: 0; elemento inesperado (URI:"", local:"email"). Los elementos esperados son <{}userVo>]

The only way I have found, is receiving a hashmap (clients send a "APPLICATION_FORM_URLENCODED" type) and build the object but I think that it is less efficient.

Any idea with JAX-RS? Is it possible?

For the response I use Gson and returns the object to the way I want:

json = gson.toJson(userVo);
return Response.ok(json, MediaType.APPLICATION_JSON).build();

--

{
    email:"[email protected]",
    mobilePhone:1234456
}

Upvotes: 1

Views: 1930

Answers (1)

Jitender Chahar
Jitender Chahar

Reputation: 204

You do not need to include the class name while sending the payload in post request.

JAX-RS is specifications provided by java. Third party vendors like Jersey , Apache CXF, RestEasy, Restlets implements JAX-RS.

For example if you are using Jersey then use jersey moxy for Marshalling/Unmarshalling. Below is the dependecy for moxy in pom.xml/ download it from here

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>

If you are using Apache cxf then use jackson-jaxrs for Marshalling/Unmarshalling. Download link

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.1.1</version>
</dependency>

Then you can post the payload as

{
    "email":"[email protected]",
    "mobilePhone":"1234456"
}

API and UserVo remains as it is.

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser (UserVo userVo){
    ...
    ...
}

@XmlRootElement
public class UserVo implements Serializable, Comparable<UserVo>{
    ...
    ...
}

Now depending upon your jax-rs implementation add appropriate library for Marshal/Unmarshal and code should work.

Upvotes: 2

Related Questions