【问题标题】:HTTP 400 Bad Request on REST request using Spring Mvc使用 Spring Mvc 对 REST 请求的 HTTP 400 错误请求
【发布时间】:2014-11-02 17:54:47
【问题描述】:

当我调用我的 REST“Hello world”服务器时,我收到一个 http 响应 400 Bad Request。

我的应用程序像 MVC 应用程序一样使用 Spring。

这是我的控制器:

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);


    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public @ResponseBody String getHello(@RequestParam("request") String json,
            HttpServletRequest request) {

        return "Hello world";
    }
}

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

这是我的 root-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>

这是我的 servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />    
    <context:component-scan base-package="uk.co.dango" />

    <tx:annotation-driven transaction-manager="txnManager"/>

    <context:annotation-config />

    <mvc:default-servlet-handler />

    <mvc:view-controller path="/index" view-name="home"/>

</beans>

我使用来自 Firefox 的 Rest 客户端发出请求,调用以下 URL:

http://localhost:8080/dango/hello

这是身体:

request={"test"=1}

这是内容类型:

content-type=application/x-javascript

如果我更改 URL 中的任何内容,我会得到 Http 404,所以我知道该 URL 至少是正确的。确实我收到了 Http 400。但是我能做些什么来解决这个问题?

【问题讨论】:

  • 您希望请求正文映射到什么以及为什么?
  • 您的控制器正在等待以request=someParam 为主体的/hello 请求。这就是@RequestParam 的意思。
  • request 可以是任何字符串,因为我只希望控制器回答“Hello World”
  • 是的,对不起,我已经看到了。但是,它仍然需要一个有效的 POST 表单参数。不是 JSON。
  • 如果您删除此编码并使用正确的 application/x-www-form-urlencoded 会怎样?

标签: java spring rest


【解决方案1】:

目前还不清楚您期望发生什么。这个

@RequestMapping(value = "/hello", method = RequestMethod.POST)
public @ResponseBody String getHello(@RequestParam("request") String json,
        HttpServletRequest request) {

@RequestParam 需要一个请求参数。请求参数定义为 URL 查询字符串元素或表单提交中的元素。

你正在发送

POST http://localhost:8080/dango/hello
content-type=application/x-javascript

request={"test"=1}

既没有表单提交也没有查询字符串。由于 @RequestParamrequired 属性默认设置为 true,因此 Spring 会以 400 Bad Request 响应(它找不到为其提供的值)。

正如JB Nizet 所建议的,将您的内容类型更改为

application/x-www-form-urlencoded

表示表单提交。或者在查询字符串中传递参数

http://localhost:8080/dango/hello?request=something

您可能还想查看@RequestBody

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 2017-12-20
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多