Springmvc rest 传递json报415错误(The server refused this request because the request entity is in a forma

Springmvc rest 传递json415错误

报错如下:(The server refused this request because the request entity is in a format 

not supported by the requested resource for the requested method.) 

----------------------------------------------------------------------------------------------------------------------

解决:

1、后台代码:

1HelloController

@RestController

@RequestMapping(value="/hello")

public class HelloController {

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

consumes = "application/json")

@ResponseBody 

    public TeacherVO greeting(@RequestBody TeacherVO t) { 

        System.out.println(t.getName());

        return t;

    }

}

2TeacherVO

public class TeacherVO {

private String id ;

private String name ;

private String age ;

public TeacherVO(){

}

public TeacherVO(String id, String name, String age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

}

2、项目类路径需包含json类库,jackson-databind-2.7.5.jarjackson-annotations-2.7.0.jarjackson-core-2.7.5.jar

3、项目配置文件中应包含<mvc:annotation-driven />,即启用了注解识别。  

4、确保post man访问时设置了header中的Content-Type值为application/json

再次使用post man访问,则可以正常访问了。

本文转自:https://blog.csdn.net/alane1986/article/details/72778700

猜你喜欢

转载自blog.csdn.net/Freya0110/article/details/79916221