M. Atif Riaz
M. Atif Riaz

Reputation: 502

Spring bind a bean to rest post request

I have following REST service build around Spring. I want to implement a POST method which can add user. The data for user is in request body and can be either in JSON/XML. I want server side implementation

  1. to automatically map incoming data to Java bean
  2. and handle both type of data (JSON/XML) using single implementation.

I have tried with @ModelAttribute in addUser method of UserController but getting all fields empty in user object. Any clue?

Here is Spring configuration file

<mvc:annotation-driven />

<context:component-scan base-package="com.rest.sg.controller" />

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>

    <property name="defaultViews">
        <list>
            <!-- JSON View -->
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
            </bean>
            <!--  XML view -->
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                        <property name="classesToBeBound">
                            <list>
                                <value>com.rest.sg.bean.User</value>
                            </list>
                        </property>
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </property>
    <property name="ignoreAcceptHeader" value="true" />

</bean>

UserController class and addUser method

@Controller
public class UserController {

    @RequestMapping(method=RequestMethod.POST, value="/user")
    @ResponseBody
    public User addUser(@ModelAttribute("user") User user) {
        userService.addUser(user);
        return user;
    }

}

And User Bean

@Entity
@XmlRootElement
@Table(name = "user", catalog = "blahblah")
public class User implements java.io.Serializable {

    // Fields
    // Getter , Setters
    ...

}

Upvotes: 1

Views: 4839

Answers (2)

Kevin Rave
Kevin Rave

Reputation: 14436

Do like this:

  @RequestMapping(value="/persons", method = RequestMethod.POST, 
                headers="Accept=*/*", 
                consumes="application/json")
public User addUser( @RequestBody User u){
              //Code.
}

When sending request, send the header

Content-Type:application/json //This is important
Accept: application/json

Upvotes: 0

a better oliver
a better oliver

Reputation: 26828

Your controller needs to know how to map the data. The default is mapping request parameters on the object's properties.

If you send JSON that represents the User object you can try

public User addUser(@RequestBody User user) {

Upvotes: 1

Related Questions