HTTP 400使用Spring Mvc对REST请求的错误请求

问题描述:

当我打电话给我的REST“Hello world”服务器时,我得到一个http响应400错误请求。HTTP 400使用Spring Mvc对REST请求的错误请求

我的应用程序与Spring一样适用于MVC应用程序。

这里是我的控制器:

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

这里是我的根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上下文。 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客户端的请求,调用以下网址:

http://localhost:8080/dango/hello 

这是身体:

request={"test"=1} 

这是内容类型:

content-type=application/x-javascript 

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

+0

您希望请求主体能够映射到什么地方?为什么? – 2014-11-02 17:57:34

+0

你的控制器正在期待以'request = someParam'为主体的'/ hello'请求。这就是@RequestParam的含义。 – 2014-11-02 17:57:56

+0

不,这是一个POST – user1883212 2014-11-02 17:59:35

这并不完全清楚你期望发生什么。 This

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

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

你发送

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

request={"test"=1} 

既不具有表单提交,也没有查询字符串。由于默认情况下,@RequestParamrequired属性设置为true,因此Spring会以400错误请求(它找不到要提供的值)作为响应。

由于JB Nizet曾建议,内容类型更改为

application/x-www-form-urlencoded 

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

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

您可能还想查看@RequestBody