我有两个控制器方法如下所示,当我向其发布JSON时,/garages/a
有效(响应为200)但/garages/a/brand
给出了404。
它们仅因映射值而不同。
我正在使用Spring 4.1.4.RELEASE和Java配置。我没有使用Spring Boot。
控制器方法:
@RequestMapping(value = "/garages/a", method = RequestMethod.POST, headers = "Content-Type = application/json", produces = "application/json")
public Garage specificGaragePost(
@RequestBody Garage garage) {
return garage;
}
@RequestMapping(value = "/garages/a/brand", method = RequestMethod.POST, headers = "Content-Type = application/json", produces = "application/json")
public Garage specificGarageBrandPost(
@RequestBody Garage garage) {
return garage;
}
车库类
public class Garage {
private String id;
private String brand;
private List<Fuel> fuels;
private double[] location;
//Getters and setters left out for brevity
示例JSON(适用于/ garages / a URL但不适用于/ garages / a / brand)
{
"id": "some-id",
"brand": "brand",
"fuels": [],
"location": [0,0]
}
答案 0 :(得分:1)
问题在于@RequestMapping sintax。删除标题部分中的空白:
@RequestMapping(value = "/garages/a", method = RequestMethod.POST, headers = "Content-Type=application/json", produces = "application/json")
@RequestMapping(value = "/garages/a/brand", method = RequestMethod.POST, headers = "Content-Type=application/json", produces = "application/json")
更好的是,使用特定于此目的的消耗,而不是使用标头来过滤Content-Type:
@RequestMapping(value = "/garages/a", method = RequestMethod.POST, consumes= "application/json", produces = "application/json")
@RequestMapping(value = "/garages/a/brand", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
答案 1 :(得分:0)
因为当它找到/garages/a/brand
时,会将其视为带有参数品牌的/garages/a/
,但/garages/a/
不接受任何参数。