Introducing Spring Auto REST Docs - Spring IO 2017
The document discusses Spring Auto REST Docs, highlighting its development and features for generating API documentation automatically from tests. It details how to create a weather API example using Spring Boot, and emphasizes the importance of specification-driven and test-driven documentation. Additionally, it showcases the integration of Swagger/OpenAPI and illustrates the testing process using Spring REST Docs for generating accurate and maintainable documentation.
@spring_io #springio17 Scalable Capital • Europe’sfastest growing Digital Wealth Manager • Authorized financial institute in Germany and the UK • From scratch with Spring Boot • Joined effort with Juraj Misur @juraj_misur
@spring_io #springio17 AsciiDoc [[resources-weather]] = Weather foryour city `POST /weather` Up-to-date temperature for the given city == Response structure include::{snippets}/weather/response-fields.adoc[] == Example request/response include::{snippets}/weather/curl-request.adoc[] include::{snippets}/weather/http-response.adoc[]
@spring_io #springio17 Constraints class WeatherRequest { /** *Country code, e.g. ES, DE, US. */ @NotNull @IsoCountryCode private String country; /** * City name. */ @NotBlank private String city; } Path Type Optional Description country String false Country code. Must be an ISO country code. city String false City name.
@spring_io #springio17 Constraints class WeatherRequest { /** *Country code, e.g. ES, DE, US. */ @NotNull @IsoCountryCode(groups = Iso.class) @CountryName(groups = Plain.class) private String country; } Path Type Optional Description country String false Country code. ISO: Must be an ISO country code. Plain: Must be a country name.
34.
@spring_io #springio17 Enums class WeatherRequest { /** *Country code, e.g. ES, DE, US. */ @NotNull private Country country; /** * City name. */ @NotBlank private String city; } Path Type Optional Description country String false Country code. Must be one of [DE, ES, FR, PT, US]. city String false City name.
@spring_io #springio17 Extension: Javadoc onmethod /** * Up-to-date weather data for cities around the globe. */ @PostMapping("weather") public WeatherResponse weather( @RequestBody @Valid WeatherRequest weatherRequest) { return new WeatherResponse(20); } 2.8. Weather POST /weather Up-to-date weather data for cities around the globe.
@spring_io #springio17 Extension: Path Parameters /** *Up-to-date weather data for cities around the globe. * * @param country Country code. * @param city City name. */ @GetMapping("weather/{country}/{city}") public WeatherResponse weatherPath( @PathVariable @IsoCountryCode String country, @PathVariable String city) { return new WeatherResponse(20); }
@spring_io #springio17 Extension: Query Parameters /** *Up-to-date weather data for cities around the globe. * * @param country Country code. * @param city City name. */ @GetMapping("weatherParam") public WeatherResponse weatherParam( @RequestParam @IsoCountryCode String country, @RequestParam String city) { return new WeatherResponse(20); }
@spring_io #springio17 Section Snippet [[resources-weather]] === Weatherfor your city `POST /weather` Up-to-date temperature for the given city ===== Request structure include::{snippets}/weather/request-fields.adoc[] ===== Response structure include::{snippets}/weather/response-fields.adoc[] ===== Example request/response include::{snippets}/weather/curl-request.adoc[] include::{snippets}/weather/http-response.adoc[] include::{snippets}/weather/section.adoc[] Documentation path Spring MVC Controller Javadoc Method name