【问题标题】:Spring controller not accepting application/jsonSpring 控制器不接受应用程序/json
【发布时间】:2014-06-07 08:11:57
【问题描述】:

当我从 Chrome Rest 客户端传递应用程序/json 参数时,我收到 400 bad request 错误。 当我为 @RequestParam 添加 required=false 时,请求被 Controller 接受,但值为 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);
}

我的 JSON 请求如下,Content-Type 是“aplication/json”

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

我尝试使用 headers="Accept=application/json",但没有太大帮助。

我的 dispatcher-servlet 是

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

我的 pom.xml 是

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

非常感谢任何帮助

【问题讨论】:

  • "surveyName"="sd" 不应该是 "surveyName":"sd" 吗?
  • 感谢您的快速回复!抱歉,打错字了,我把它改成了 "surveyName":"sd" 问题依然存在

标签: spring spring-mvc http-status-code-404 rest-client


【解决方案1】:

在您的情况下,您可以使用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

希望这能有用!

【讨论】:

  • 是的,使用地图。您正在寻找 RequestParams,但 json 中的值不是请求参数,而是请求正文的一部分。
  • 非常感谢。我尝试了 Map 和 Pojo 方法,都为我解决了!!
  • 请告诉我如何传递日期,当我将 JSON 传递为“{”surveyName“:”1”时,我在 Pojo 类中使用了 Date 变量,例如“private java.util.DatesurveyDate” ,“surveyDesc”:“123”,“surveyDate”:“01-01-2014”}”。我尝试添加注释“@DateTimeFormat(pattern="MM-DD-YYYY")”,但没有太大帮助。
  • 谢谢..在同时使用 @JsonSerialize(using = CustomDateSerializer.class) 和 @JsonDeserialize(using = CustomDateDeserializer.class) 后,这对我来说很有效
【解决方案2】:

@RequestParam 不能用于加载复杂 json 对象的一部分。它设计用于选择request parameter,而不是在(单个)请求参数中选择某些内容。

你需要使用@RequestBody和一个容器对象

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

  ...
}

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

或者您可以实施 Biju Kunjummen 在他的answer 中描述的类似问题的解决方案。这个想法是实现你自己的HandlerMethodArgumentResolver,它由一个参数注释触发,该注释采用 JsonPath 表达式参数

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

查看Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax 了解实现细节。

如果你喜欢这个答案,也请点赞Biju Kunjummen answer,因为这是他的主意。我只是看了一会儿,因为这是一个有趣的问题。

【讨论】:

    【解决方案3】:

    还可以在 lib 中添加这两个库或为此添加 maven。

    jackson-jaxrs-1.6.1.jar

    jackson-mapper-asl-1.9.9.jar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      相关资源
      最近更新 更多