好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

HTTP 415错误-Unsupported media type详解

   前段时间在使用@RequestBody注解的时候遇到了一个以前没遇到过的错误,HTTP 415  Unsupported media type? 这个是个什么鬼,@ResponseBody可以正常工作而一使用@RequestBody来进行交互就会报这个错误。一直请求不到Controller,我开始总以为是路径或者json格式不对的问题,上网查资料大多也说的是这个问题。可是我已经写了

data : JSON.stringify(user),  

dataType : 'json',

contentType : 'application/json;charset=UTF-8',

按照网上的办法也一直不管用,百思不得其解。于是继续在网上找资料,

网上分析原因很多,但找了很久都没解决,基本是以下几类:

springmvc添加配置、注解; pom.xml添加jackson包引用; Ajax请求时没有设置Content-Type为application/json  发送的请求内容不要转成JSON对象,直接发送JSON字符串即可

各种办法都尝试了一遍,还是没有能解决问题;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<script>

     jQuery( function ($){

     var urlStr = "<%=request.getContextPath()%>/user/GetUser" ;

         var user = {

             "id" : 6,

             "userName" : "小红" ,

             "password" : "123" ,

             "age" : 12

         };

         $.ajax({

             url : urlStr,

             type : "POST" ,

             data : JSON.stringify(user), //转JSON字符串 

             dataType : 'json' ,

             contentType : 'application/json;charset=UTF-8' , //contentType很重要    

             success : function (result) {

                 console.log(result);

                 //alert(result);

                 //data = eval("(" + result + ")");

                 //alert(data);

                 $( "#a" ).html(result.userName);

             }

         });

     });

</script>

造了一个简单是数据来测试,还是不行。。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

package com.cn.hnust.controller;

 

import javax.servlet.http.HttpServletRequest;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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.cn.hnust.domain.User;

import com.cn.hnust.service.IUserService;

 

@Controller

@RequestMapping ( "/user" )

public class UserController {

 

     @Autowired

     private IUserService userService;

 

     @RequestMapping ( "/showUser" )

     public String toIndex(HttpServletRequest request, Model model) {

         // int userId = Integer.parseInt(request.getParameter("id"));

         // User user = this.userService.getUserById(userId);

         // model.addAttribute("user", user);

         return "showUser" ;

     }

 

     @RequestMapping (value = "/GetUser" , method = RequestMethod.POST)

     public @ResponseBody

     User GetUser( @RequestBody User user) {

         user.setUserName( "Wei" );

         return user;

     }

 

}

控制器也很简单,可是就是请求不到Controller方法。于是我继续在网上寻找资料,直到看到一篇博客,才找到了问题的解决办法。

原来是Jackson的依赖问题,spring3.x和spring4.x是不同的:

spring3.x是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

spring4.x是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

具体可以查看spring-web的jar确认,哪个存在用哪个!

在配置ViewResolver的时候应该指定响应的版本,于是我将springmvc的配置文件改为:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

< bean

         class = "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" >

         < property name = "order" value = "1" />

         < property name = "mediaTypes" >

             < map >

                 < entry key = "json" value = "application/json" />

                 < entry key = "xml" value = "application/xml" />

                 < entry key = "htm" value = "text/html" />

             </ map >

         </ property >

 

         < property name = "defaultViews" >

             < list >

                 <!-- JSON View -->

                 < bean

                     class = "org.springframework.web.servlet.view.json.MappingJackson2JsonView" >

                 </ bean >

             </ list >

         </ property >

         < property name = "ignoreAcceptHeader" value = "true" />

     </ bean >

仅仅将

?

1

MappingJacksonJsonView

改为

?

1

MappingJackson2JsonView

到此这篇关于HTTP 415错误-Unsupported media type详解的文章就介绍到这了,更多相关HTTP 415错误-Unsupported media type内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/kingtracy8/article/details/78076024

查看更多关于HTTP 415错误-Unsupported media type详解的详细内容...

  阅读:76次