我无法弄清楚为什么在将POST请求传递给http://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
答案 0 :(得分:6)
你会注意到你的方法是用
注释的@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
consumes
是对@RequestMapping
的限制,称此方法仅处理Content-Type
application/json
的请求。您的请求似乎没有该标头。你需要添加它。