Jemo
Jemo

Reputation: 309

how to handle Json body in post request in jax-rs

I have a project (homework) about JAX-RS. I'm working with NetBeans, Jersey and Tomcat.

enter image description here

This is my "User" class for main object in the system.

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="user")
public class User {
    
    //@XmlElement
    //public int id ;
    @XmlElement
    public String username;
    @XmlElement
    public String fullname;
    @XmlElement
    public String gender;
    @XmlElement
    public String birthDate;
    
    public User(){
        
    }
    
    public User(String username,String fullname, String gender,String birthDate){
        
        //this.id = id;
        this.username = username;
        this.fullname = fullname;
        this.gender = gender;
        this.birthDate = birthDate;
    }
    
}

This is my "JAXBContextResolver" Class

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;


@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext>{
    
    private JAXBContext context;
    private Class[] types = {User.class};
    
    
    public JAXBContextResolver() throws Exception {
        
        this.context = 
        new JSONJAXBContext(  JSONConfiguration.mapped().build(), types); 
    }
                    
    @Override
    public JAXBContext getContext(Class<?> objectType) {
         for (Class type : types) {
             if (type == objectType) {
                 return context;
             }
         }
        
        return null;
        
    }
    
}

And this is my post method in the "UserService" class

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON) 
    public List<User> createNewUser(User tUser)  {
        List<User> list = new ArrayList<User>();
        
        list.add(tUser);
            
            
        
        return list;
    }

When I am trying a post new user in the localhost with RESTClient (Firefox add-ons) my request body is a json input like that:

{"user":{"username":"blabla","fullname":"blabla","gender":"M","birthDate":"05.01.1978"}}

In the post method (in the UserService class) must the variable "tUser" automatically filled with the coming input ? "tUser" variable shows null elements in it in the debugging mode like that:

enter image description here

If I know wrong could somebody correct me please? Why this values shows null? Must not them shows "blabla" - "blabla" - "M" - "05.01.1878" ? Could you help me please?

Upvotes: 3

Views: 7801

Answers (2)

Ilya
Ilya

Reputation: 29663

@XmlRootElement is not working in your example. Send

{"username":"blabla","fullname":"blabla","gender":"M","birthDate":"05.01.1978"}  

instead
EDIT
1)

public List<User> createNewUser(Request tUser)  

and class

class Request
{
   public User user;
}  

2)

public List<User> createNewUser(String tUser)  

and convert String to object using google-gson or jackson json processor

Upvotes: 0

Jemo
Jemo

Reputation: 309

I solved this problem; In the JAXBContextResolver class I change the method like that :

public JAXBContextResolver() throws Exception {

        this.context = 
        new JSONJAXBContext(  JSONConfiguration.mapped().rootUnwrapping(false).build(), types); 
}

The difference with the first one is adding "rootUnwrapping(false)" expression.

Upvotes: 2

Related Questions