使用我创建的 api 时获取 415 不受支持的媒体类型
Getting 415 unsupported media type while consuming an api which I created
@RequestMapping(value="/sam", method=RequestMethod.POST, consumes = {"text/plain", "application/*"})
public void test(HttpServletRequest request, HttpServletResponse response, @RequestBody Test test) {
try {System.out.println("*******************inside**************************");
} catch(Exception exception) {
exception.printStackTrace();
}
}
public class Test {
private String desc;
private int id;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
要求body我用的是
{
"id": 88,
"desc":"test"
}
即使我在请求映射中添加了消耗,但当我尝试从邮递员访问时我得到了 415,而且我在邮递员中设置了请求 header,content-type 为 application/json
我错过了 json 转换器库,所以当我在我的 pom.xml 中添加 jackson-databind maven 依赖项时它起作用了。
@RequestMapping(value="/sam", method=RequestMethod.POST, consumes = {"text/plain", "application/*"})
public void test(HttpServletRequest request, HttpServletResponse response, @RequestBody Test test) {
try {System.out.println("*******************inside**************************");
} catch(Exception exception) {
exception.printStackTrace();
}
}
public class Test {
private String desc;
private int id;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
要求body我用的是
{
"id": 88,
"desc":"test"
}
即使我在请求映射中添加了消耗,但当我尝试从邮递员访问时我得到了 415,而且我在邮递员中设置了请求 header,content-type 为 application/json
我错过了 json 转换器库,所以当我在我的 pom.xml 中添加 jackson-databind maven 依赖项时它起作用了。