我正在使用
function doAjaxPost() {
var user= {
nameA: $('#name').val(),
emailA: $('#email').val(),
};
$.ajax({
type : "post",
url: "hello.htm",
data: JSON.stringify(user),
contentType: 'application/json',
success : function(response) {
alert(response);
},
error : function(e) {
alert('Error: ' + e);
}
});
}
// controller
@RequestMapping(value="/hello", method = RequestMethod.POST)
public @ResponseBody List<String> hello11(@RequestBody UserBean user)
{
System.out.println(user.getEmailA());
List<String> listObj=new ArrayList<String>();
listObj.add("jay mata dee");
return listObj;
}
答案 0 :(得分:0)
更改你的ajax中的url,然后尝试...
url:&#34;你好&#34;,
答案 1 :(得分:0)
您需要在配置中定义MessageConverter。 Spring必须知道如何创建请求的 application / json @ResponseBody
@Configuration
@EnableWebMvc
public class MyWebConfiguration extends WebMvcConfigurerAdapter {
[...]
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new ResourceHttpMessageConverter());
converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
converters.add(new MappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
}
您还需要包含一些映射库:
的pom.xml
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>