我是Spring的新手,我一直在努力找出将JSON发送到Spring REST控制器的最佳方法。我已经在StackExchange上阅读了一些类似的问题,但尝试解决方案似乎没有帮助,所以我似乎仍然遗漏了一些东西。
在春天方面:
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
UserService userService;
@RequestMapping(value="/register",method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody UserWrapper registerUser(@RequestBody RegisterParams params) {
logger.info("REGISTER POST!");
User newUser = userService.registerUser(params.getEmail(),params.getPassword(),params.getName());
return new UserWrapper(newUser);
}
private class RegisterParams implements Serializable {
private String email;
private String password;
private String name;
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
在javascript方面(名称,密码和电子邮件变量包含字符串):
jQuery.ajax({
type: "POST",
url: '/gameserver/user/register',
data: JSON.stringify({name:name,password:password,email:email}),
success: onSuccess,
error:onError
,contentType:'application/json'
,dataType:'json'
});
镀铬输出:
Request URL:http://192.168.56.101:8080/gameserver/user/register
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:71
Content-Type:application/json
Host:192.168.56.101:8080
Origin:http://192.168.56.101:8080
Referer:http://192.168.56.101:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{name:nickname, password:somepass, email:something@email.com}
Response Headersview source
Connection:close
Content-Language:en
Content-Length:986
Content-Type:text/html;charset=utf-8
Date:Fri, 20 Jun 2014 11:18:17 GMT
Server:Apache-Coyote/1.1
HTTP状态400 -
输入状态报告
消息
description客户端发送的请求是语法上的 不正确。
我很感激帮助,如果我错过了类似帖子的内容,我很抱歉。
答案 0 :(得分:0)
尝试将RegisterParams
的范围更改为public static
。我对Spring JSON映射没有太多经验,但是其他框架默认情况下不能很好地处理私有嵌套类。
MappingJacksonHttpMessageConverter
?有关同时使用Jackson和Spring MVC的更多信息,请参阅this SO。
答案 1 :(得分:-1)
您需要在此处利用Jackson Libraries,注册MappingJackson2HttpMessageConverter并在RequestMappingHandlerAdapter中注入它。这就是我使用它的方式 在弹簧配置文件中使用它 -
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonRequestConverter" />
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="jsonRequestConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
如果您使用的是maven,则可以对Jackson Libraries使用以下依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>