zoukankan      html  css  js  c++  java
  • 问题.springmvc错误.415:Unsupported Media Type

    场景是在希望用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

  • 相关阅读:
    【Vue原理】Compile
    vue v-cloak 的作用和用法
    vue中template的作用及使用
    Vue-router 嵌套路由
    Vue keep-alive实践总结
    Vuex入门(2)—— state,mapState,...mapState对象展开符详解
    mysql允许外部连接设置
    Swagger入门教程
    牛客枚举题---铺地毯
    牛客区间求和、枚举、贪心题---数学考试
  • 原文地址:https://www.cnblogs.com/shanhm1991/p/7194242.html
Copyright © 2011-2022 走看看