Java 400 Bad Request - 将 JSON 数据发布到使用 Spring MVC 实现的 RESTful 控制器时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23559874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:54:45  来源:igfitidea点击:

400 Bad Request - while POSTing JSON data to RESTful controller implemented with Spring MVC

javajsonrestspring-mvc

提问by Akshay Lokur

I am trying to POST some JSON data to RESTful spring controller. But getting "400 Bad Request" as response status.

我正在尝试将一些 JSON 数据发布到 RESTful spring 控制器。但是得到“400 Bad Request”作为响应状态。

Giving below code from the key configuration files which I am using for this:

从我为此使用的关键配置文件中提供以下代码:

pom.xml dependencies:

pom.xml 依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.core</groupId>
            <artifactId>Hymanson-core</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.core</groupId>
            <artifactId>Hymanson-databind</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.1.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

dispatcher-servlet.xml:

调度程序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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.lokur.controllers" />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
                <entry key="xml" value="text/xml" />
            </map>
        </property>
        <property name="defaultContentType" value="text/html" />
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingHymanson2JsonView" />
            </list>
        </property>
    </bean>

</beans>

Controller class:

控制器类:

package com.lokur.controllers;

import com.lokur.dto.Employee;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/employees")
public class EmployeeController {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void saveEmployee(@RequestBody Employee employee, HttpServletResponse response) {

        System.out.println("Saving employee...");
        System.out.println("Emp ID = " + employee.getId());
        System.out.println("Emp Name = " + employee.getName());

        // Set dummy "location" for this newly created Employee resource in response header
        response.setHeader("Location", "/employees/"+employee.getId());
    }

}

Request details:

索取详情:

URL:http: //localhost:8081/RestfulWSPOC/employees

URL:http://localhost:8081/RestfulWSPOC/employees

Method:POST

方法:POST

Headers:Accept: application/json

标头:接受:application/json

Payload:

有效载荷:

{
    id: 300999

    name: "Akshay Lokur"

    designation: "Associate"

    department: "Tech"

    salary: 500000
}

Thanks.

谢谢。

采纳答案by Akshay Lokur

I got the issue:

我得到了这个问题:

The problem was mere syntax issue with my JSON request (had missed commas!).

问题仅仅是我的 JSON 请求的语法问题(错过了逗号!)。

I corrected it like below and it is working like a charm now:

我像下面那样更正了它,现在它就像一个魅力:

{
    "id": 300999,
    "name": "Akshay Lokur",
    "designation": "Associate",
    "department": "Tech",
    "salary": 500000
}

Response:

回复:

201 Created

P.S.: We also need to give double quotes to "keys" in JSON!

PS:我们还需要给JSON中的“keys”加上双引号!

Yippee :)

伊皮 :)

Cheers.

干杯。