场景是在希望用ajax发post请求,传递一个json对象,在controller中直接使用java对象接收时遇到的,具体错误信息如下:
{ "timestamp": 1500272249207, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", "path": "/addSuggestions" }
原因如错误信息里所说:Content type不支持。将Content type改为:application/json可以解决问题。
本人使用postman模拟的http请求,简单如下:
对应controller处理:
package cn.migu.battle.controller; import org.springframework.beans.factory.annotation.Autowired; 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.RestController; import cn.migu.battle.constant.InterfaceConstant; import cn.migu.battle.model.UserSuggestionModel; import cn.migu.battle.service.UserSuggestionService; import cn.migu.battle.utils.JsonResponseForList; /** * * @author Administrator * */ @RestController public class UserSuggestionController { @Autowired private UserSuggestionService service; @RequestMapping(value = "/addSuggestions",method = {RequestMethod.POST}) public JsonResponseForList addSuggestion(@RequestBody(required = false) UserSuggestionModel model){ if(!service.addSuggestions(model)){ return new JsonResponseForList(InterfaceConstant.STATUS_FAILURE,InterfaceConstant.MESSAGE_FAILURE,null); } return new JsonResponseForList(InterfaceConstant.STATUS_SUCCESS,InterfaceConstant.MESSAGE_SUCCESS,null); } }
具体解释不再赘述,参考链接:
http://blog.csdn.net/LostSh/article/details/68923874
http://blog.csdn.net/ccc20134/article/details/45094679