The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
经过排查原来是配置文件方式不同引起的:
首先引入
jackson-core-asl-1.6.4.jar
jackson-mapper-asl-1.6.4.jar
如果是:
<mvc:annotation-driven />
那么则没有问题
如果把配置换成
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
则会报异常。
那么需要补充配置
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
------------------------------------------------------------
- package com.controller;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.pojo.Users;
- @Controller
- public class JSONController {
- @RequestMapping(value = "getUserListFromServer", method = RequestMethod.GET)
- @ResponseBody
- public Map<String, Object> getUserList() {
- Map<String, Object> modelMap = new HashMap<String, Object>(3);
- modelMap.put("total", "1");
- modelMap.put("data", getUsers());
- modelMap.put("success", "true");
- return modelMap;
- }
- public List<Users> getUsers() {
- List<Users> list = new ArrayList<Users>();
- for (int i = 0; i < 5; i++) {
- Users user = null;
- user = new Users();
- user.setName("Pandy " + i);
- user.setPassword("pandy " + i);
- user.setEmail("panyongzheng@163.com " + i);
- user.setRemarks("Nothing " + i);
- user.setId(i);
- list.add(user);
- }
- return list;
- }
- @RequestMapping(value = "commitToServer", method = RequestMethod.POST)
- @ResponseBody
- public Map<String, String> addUser(@RequestBody Users user) {
- System.out.println(user.getName());
- Map<String, String> map = new HashMap<String, String>(1);
- map.put("success", "true");
- return map;
- }
- }
- getFromServer = function() {
- alert("getFromServer");
- $.ajax({
- type : "GET",
- url : "getUserListFromServer.do",
- data : "name=John&location=Boston",
- contentType : 'application/json', //这里可以不加上,但是要是提交的是json对象字符串的话,必须加上.
- dataType : 'json',//这里可以不加上,但是要是提交的是json对象字符串的话,必须加上.
- success : function(obj) {
- alert("Data Saved: " + JSON.stringify(obj));
- },
- error: function(obj, s1, s2){
- alert(JSON.stringify(obj));
- },
- complete : function(obj, s1){
- alert("complete:"+s1);
- }
- });
- }
- commitToServer = function() {
- alert("commitToServer");
- var obj = {};
- obj.name="Pandy";
- obj.email="test@163.com";
- var param = JSON.stringify(obj);
- alert(param);
- $.ajax({
- type : "POST",
- url : "commitToServer.do",
- data : param,
- contentType : 'application/json', //这里必须要加上.
- dataType : 'json',
- success : function(obj) {
- alert(JSON.stringify(obj));
- },
- error: function(obj, s1, s2){
- alert(JSON.stringify(obj));
- },
- complete : function(obj, s1){
- alert("complete:"+s1);
- }
- });
- }