Http status 415 Unsupported Media Type

What is 415 ?

  • HTTP 415 Unsupported Media Type

    The client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

Let’s look at the broswer ?

  • Http status 415 Unsupported Media Type_第1张图片

How to resolve 405 problem when using ajax post @ResponseBody return json data ?

  • if you use Spring 4.x jar,please following me(Maven Repository-Spring 4.x.jar)

  • add related jar package

    spring-webmvc.x.x.jar
    jackson-databind.jar
    jackson-core.jar
    jackson-annotations.jar

  • In Spring Configuration file,add following code

<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.
MappingJackson2HttpMessageConverter">  
    <property name="supportedMediaTypes">  
        <list>  
          <value>application/json;charset=UTF-8value>  
        list>  
    property>  
bean>
  • In ajax , set contentType : ‘application/json;charse=UTF-8’
    var data = {"name":"jsutin","age":18};
    $.ajax({  
        url : "/ASW/login.html",  
        type : "POST",  
        data : JSON.stringify(data),  
        dataType: 'json',  
        contentType:'application/json;charset=UTF-8',      
        success : function(result) {  
            console.log(result);  
        }  
    }); 
  • In server ,using @RequestBody anotation receive json data,and using @ResponseBody anotation response json to jsp page
@RequestMapping(value = "/login",method = RequestMethod.POST)
public @ResponseBody User login(@RequestBody User user){
    system.out.println(user.getName);
    system.out.println(user.getAge);
}

你可能感兴趣的:(web基础)