为什么在Spring MVC中发送json请求并使用json时出现415错误?

时间:2019-06-09 16:31:06

标签: spring spring-mvc spring-rest

我正在以以下格式{"name":"ABC","id":1}

从邮递员发送JSON发布请求

,并尝试使用Employee与Controller中的@Requestbody类进行映射 但仍然出现错误,因为 HTTP状态415 –带有标题(内容类型:application / json)的不受支持的媒体类型

1)控制器:

@Controller
public class RegistrationController {

        @Autowired
    private EmployeeService employeeService;

    @RequestMapping(path="/jsonreq", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
    public String json(@RequestBody Employee e)  
    {
        System.out.println("JSON_2::");

        return "Testing";
    }

}

2)员工类别:

public class Employee {

    private int id;
    private String name;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

0 个答案:

没有答案