Spring controller not accepting application/json

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


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!


@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.


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

链接地址: http://www.djcxy.com/p/41334.html

上一篇: 编码参数在Node.js中使用请求模块

下一篇: Spring控制器不接受应用程序/ json