Ari
Ari

Reputation: 341

How can we set request content type in Spring 3.0.2?

My code is like:

 @Controller
    @RequestMapping( value = "/walley/login", method = RequestMethod.POST)
    public void login(HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody RequestDTO requestDTO)
            throws IOException, ServiceException {
              String userName = requestDTO.getUserName();
            String password = requestDTO.getPassword();
            System.out.println("userName " + userName +" :: password "+        password);}

RequestDTO.java file

public class RequestDTO {
    public String userName;
    public String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    }

Hit post request with following steps in postman.

  1. Open Postman.
  2. Enter URL in the URL bar http://localhost:8080/walley/login.
  3. Click Headers button and enter Content-Type as header and application/json in value.
  4. Select POST from the dropdown next to the URL text box.
  5. Select raw from the buttons available below URL text box.
  6. Select JSON from the following dropdown. In the textarea available below, post your request object: { "userName" : "test", "password" : "someone" }

In response I am getting error:

org.springframework.web.HttpMediaTypeNotSupportedException:Content type 'application/json' not supported

I checked and I found in Spring 3.1.X or 3.2.X we can set content type "consumers and producers" in @RequestMapping for request and it works but in 3.0.2 they are not support "consumers and producers". So how can we set request content type in Spring 3.0.2 version with @RequestBody annotation?

Upvotes: 1

Views: 1601

Answers (1)

Ari
Ari

Reputation: 341

We don't have "consumers and producers" with Spring version 3.0.2, So we need to add some additional entries in root pom.xml and dispatch-servlet.xml to use @RequestBody annotation.

pom.xml

  <!-- jackson -->
      <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-mapper-asl</artifactId>
       <version>1.9.13</version>
      </dependency>
      <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-core-asl</artifactId>
           <version>1.9.13</version>
      </dependency>

dispatch-servlet.xml

<bean id="jacksonMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
    </property>
    </bean>

And controller should be liked

@Controller
    @RequestMapping( value = "/walley/login", method = RequestMethod.POST)
    public void login(@RequestBody RequestDTO requestDTO,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServiceException {
              String userName = requestDTO.getUserName();
            String password = requestDTO.getPassword();
            System.out.println("userName " + userName +" :: password "+   password);}

Upvotes: 2

Related Questions