Spring Boot REST API Endpoint Mapping best practice

笑着哭i 提交于 2020-01-24 01:56:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!