• 问题.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

  • 相关阅读:
    powershell 更新 IIS SSL 证书
    让 .NET 轻松构建中间件模式代码(二)
    让 .NET 轻松构建中间件模式代码
    小白学数据分析----->学习注册转化率
    说说第三方服务
    小白学数据分析----->ARPPU的误区
    写在2013年最后一天
    小白学数据分析----->移动游戏的使用时长分析
    小白学数据分析----->付费用户生命周期研究
    小白学数据分析----->什么才是留存率的关键?
  • 原文地址:https://www.cnblogs.com/shanhm1991/p/7194242.html
Copyright © 2020-2023  润新知