4

When I am passing application/json parameters from Chrome Rest Client,I am getting 400 bad request error. When I add required=false for @RequestParam, the request is accepted by Controller but the values is Null.

@RequestMapping(value = "/add",
                method = RequestMethod.POST,
                consumes="application/json",
                produces="application/json")
public @ResponseBody String add(
  @RequestParam(value="surveyName") String surveyName,
  @RequestParam(value="surveyDesc")  String surveyDesc,
  ModelMap model) throws Exception
{
    System.out.println("request parameters in /add/syrvey surveyName= "+surveyName);
}

My JSON request is as below and Content-Type is "aplication/json"

{"surveyName"="sd", "surveyDesc":"sd"}

I tried using headers="Accept=application/json",but it didn't help much.

My dispatcher-servlet is

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=">
    <context:annotation-config />
    <context:component-scan base-package="com.survey.controller" />
    <context:component-scan base-package="com.survey.service" />
    <context:component-scan base-package="com.survey.dao" />
    <context:component-scan base-package="com.survey.entity" />
    <context:component-scan base-package="com.survey.constants" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>
</beans>

My pom.xml is

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>

Any help greatly appreciated

Alexander Pavlov
  • 29,538
  • 4
  • 63
  • 88
madhuram2468
  • 41
  • 1
  • 5

3 Answers3

3

In your situation you can use a Map:

public @ResponseBody String add(@RequestBody Map<String, String> input) throws Exception

and then iterate over it in order to find your parameters, or you can use a Pojo

public class Pojo {
    private String surveyName;
    private String surveyDesc;

    ...
}

public @ResponseBody String add(@RequestBody Pojo pojo) throws Exception

Hope this can be useful!

gipinani
  • 12,376
  • 10
  • 50
  • 80
  • 2
    Yes, use a map. You are looking for RequestParams, but the values in the json are not request params, but are part of the request body. – DavidA Apr 22 '14 at 20:32
  • 1
    Thanks a lot. I tried both Map and Pojo method and both worked out for me !! – madhuram2468 Apr 23 '14 at 05:20
  • Please tell me how to pass a Date, I used Date variable in my Pojo class like "private java.util.Date surveyDate", when I pass JSON as "{"surveyName":"1", "surveyDesc":"123", "surveyDate":"01-01-2014"}". I tried adding the annotation "@DateTimeFormat(pattern="MM-DD-YYYY")" but it didn't help much. – madhuram2468 Apr 23 '14 at 05:27
  • @madhuram2468 Take a look here: http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html – gipinani Apr 23 '14 at 06:00
  • 1
    Thanks..This worked out for me after using both @JsonSerialize(using = CustomDateSerializer.class) and @JsonDeserialize(using = CustomDateDeserializer.class) – madhuram2468 Apr 24 '14 at 16:15
1

@RequestParam can not be used to load parts of a complex json object. It is designed for selecting request parameter, but not to select something within a (single) request parameter.

You need to use @RequestBody and a container object

public class MyContainer {
  private String surveyName;
  private String surveyDesc;

  ...
}

public @ResponseBody String add(@RequestBody Container container){...}

Or you can implement a solution described by Biju Kunjummen in his answer to a similar question. The idea is to implement your own HandlerMethodArgumentResolver that is triggered by an parameter annotation which take a JsonPath expression argument

public @ResponseBody String add(
    @JsonArg("/surveyName") String surveyName,
    @JsonArg("/surveyDesc")  String surveyDesc){...}

Have a look at Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax for the implementation details.

If you like this answer, then please also upvote Biju Kunjummen answer, because it is his idea. I only goggled a bit because it is an interesting question.

Community
  • 1
  • 1
Ralph
  • 111,219
  • 48
  • 270
  • 362
0

also add those two library in lib or add maven for this.

jackson-jaxrs-1.6.1.jar

jackson-mapper-asl-1.9.9.jar

subhash lamba
  • 276
  • 1
  • 13