Java 400错误请求-将JSON数据发布到使用SpringMVC实现的RESTful控制器时
我试图将一些JSON数据发布到RESTful spring控制器。但获得“400错误请求”作为响应状态 给出了我用于此目的的密钥配置文件中的以下代码: pom.xml依赖项:Java 400错误请求-将JSON数据发布到使用SpringMVC实现的RESTful控制器时,java,json,rest,spring-mvc,Java,Json,Rest,Spring Mvc,我试图将一些JSON数据发布到RESTful spring控制器。但获得“400错误请求”作为响应状态 给出了我用于此目的的密钥配置文件中的以下代码: pom.xml依赖项: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</ar
<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.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-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>
<?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.MappingJackson2JsonView" />
</list>
</property>
</bean>
</beans>
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());
}
}
请求详细信息:
<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.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-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>
<?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.MappingJackson2JsonView" />
</list>
</property>
</bean>
</beans>
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());
}
}
URL:http://localhost:8081/RestfulWSPOC/employees
方法:邮寄
标题:Accept:application/json
有效载荷:
谢谢。我收到了问题:
问题仅仅是我的JSON请求的语法问题(遗漏了逗号!)
我纠正了它,如下所示,它现在就像一个符咒:
{
"id": 300999,
"name": "Akshay Lokur",
"designation": "Associate",
"department": "Tech",
"salary": 500000
}
答复:
201 Created
注:我们还需要在JSON中给“键”加双引号
伊佩:)
干杯。您能不能也展示一下Employee类,以及您看到的任何可能的stacktrace?@geoand:问题已解决。请看下面我的答案。