两种 Spring 控制器方法,一种 Returns 200,另一种 Returns 404。仅映射不同 URL
Two Spring Controller Methods, One Returns 200, Other Returns 404. Only Differ By The Mapping URL
我有两个控制器方法如下所示,当我 post 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;
}
车库class
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/品牌)
{
"id": "some-id",
"brand": "brand",
"fuels": [],
"location": [0,0]
}
问题出在@RequestMapping 语法中。去掉headers部分的空格:
@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")
更好的是,不要使用 headers 来过滤 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")
因为当它找到/garages/a/brand
时,它把它当作带有参数品牌的/garages/a/
,但是/garages/a/
不接受任何参数。
我有两个控制器方法如下所示,当我 post 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;
}
车库class
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/品牌)
{
"id": "some-id",
"brand": "brand",
"fuels": [],
"location": [0,0]
}
问题出在@RequestMapping 语法中。去掉headers部分的空格:
@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")
更好的是,不要使用 headers 来过滤 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")
因为当它找到/garages/a/brand
时,它把它当作带有参数品牌的/garages/a/
,但是/garages/a/
不接受任何参数。