Spring controller does not accept application / json

When I pass the / json application settings from the Chrome Rest client, I get 400 errors with an error. When I add required = false for @RequestParam, the request is accepted by the controller, but the values ​​are 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 below and Content-Type is "aplication / json"

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

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

My servlet dispatcher

<?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

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

Any help is much appreciated

+4
source share
3 answers

In your situation you can use Map:

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

, , Pojo

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

    ...
}

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

, !

+3

@RequestParam json. , - () .

@RequestBody -

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

  ...
}

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

, Biju Kunjummen , . , HandlerMethodArgumentResolver, , JsonPath

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

@RequestBody Spring MVC Ajax .

, , Biju Kunjummen, . , .

+1

lib maven .

-jaxrs-1.6.1.jar

--ASL-1.9.9.jar

0

Source: https://habr.com/ru/post/1537559/


All Articles