问题
I am using bellow endPoint URL Mapping with HTTP Methods like ( POST,DELETE,GET,PUT)
POST for Create a new Trade -
@PostMapping("/trade")
DELETE for Delete a Trade with specific id -
@DeleteMapping("/trade/{id}")
GET for Get details of specific Trade -
@GetMapping("/trade/{id}")
PUT for Update Trade details -
@PutMapping(“/trade/{id}”)
GET for Retrieve all Trade list of collection -
@GetMapping("/trades")
if I am missing anything here Please suggest
回答1:
Add API version like
@RestController
@RequestMapping("/API/V1")
public class TestController {
@RequestMapping("/greeting")
public String greeting( {
return "welcome";
}
}
回答2:
for versioning I can Use bellow any one approach
1.Through a URI path – you include the version number in the URL path of the endpoint, for example, /api/v1/trade.
2.Through query parameters – you pass the version number as a query parameter with a specified name, for example, /api/trade?version=1.
3.Through custom HTTP headers – you define a new header that contains the version number in the request.
4.Through a content negotiation – the version number is included in the “Accept” header together with the accepted content type.
1) different URL Mapping Something Like bellow
Version 1:-
`http://localhost:8080/v1/trade`
Version 2:-
`http://localhost:8080/v2/trade`
@RestController
public class TradeController {
@GetMapping("v1/trade")
public Trade tradeVersionOne() {
return new Trade("123","Trade Result");
}
@GetMapping("v2/trade")
public Trade tradeVersionTwo() {
return new Trade(new RealTimeTrade("123", "Real Time Trade Result"));
}
2) Query parameters use in URL Version 1:- http://localhost:8080/trade/param?version=1
Version 2:- http://localhost:8080/trade/param?version=2
@RestController
public class TradeController {
@GetMapping("v1/trade" params = "version=1")
public Trade tradeVersionOne() {
return new Trade("123","Trade Result");
}
@GetMapping("v2/trade" params = "version=2")
public Trade tradeVersionTwo() {
return new Trade(new RealTimeTrade("123", "Real Time Trade Result"));
}
}
来源:https://stackoverflow.com/questions/51035930/spring-boot-rest-api-endpoint-mapping-best-practice