我不明白为什么在将 POST 请求传递给时会收到 HTTP 415http://localhost:8080/company
我在 POST 请求中的 JSON
{
"id" : 7,
"name" : "IBM"
}
这是我在控制器中的方法
@Controller
@RequestMapping("/company")
public class CompanyController {
@Autowired
CompanyRepository companyRepository;
@RequestMapping(method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Collection<Company> getAll() {
return companyRepository.getCompanies();
}
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
public String add(@RequestBody Company company) {
companyRepository.save(company);
return "redirect:/company";
}
}
还有我的实体
@Entity
@Table(name = "company")
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@JsonIgnore
@OneToMany(mappedBy = "company")
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Employee> employees;
有什么想法如何修复它吗?
UPD:
响应消息:The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
你会注意到你的方法被注释为
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
The consumes
是一个限制@RequestMapping
说这个方法只会处理具有Content-Type
of application/json
。您的请求似乎没有该标头。你需要添加它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)