Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wie soll man das denn nutzen? - Spring REST Docs

Gerrit Meier
September 30, 2016

Wie soll man das denn nutzen? - Spring REST Docs

REST API Dokumentation mit Spring REST Docs

Gerrit Meier

September 30, 2016
Tweet

More Decks by Gerrit Meier

Other Decks in Programming

Transcript

  1. Wie soll man das denn nutzen? Dokumentation von REST-Schnittstellen mit

    Spring REST Docs Gerrit Meier@JUG Saxony Day 2016
  2. Senior Consultant
 T-Systems on site services GmbH 8 Jahre ‚professionell‘

    Java JUG Ostfalen Co-Organisator Podcast meistermeier
  3. @ApiOperation(nickname = "loginUser",
 value = "Logs user into the system",

    response = classOf[String], 
 httpMethod = "GET")
 @ApiResponses(Array(
 new ApiResponse(code = 400, message = "Invalid username and password combination")))
 def loginUser(
 @ApiParam(value = "The user name for login", required = true)
 username: String,
 @ApiParam(value = "The password for login in clear text", required = true) 
 password: String) = Action { 
 implicit request =>
 JsonResponse("logged in user session:" + System.currentTimeMillis())
 }
  4. 
 @ApiOperation(nickname = "updateUser",
 value = "Updated user“, notes =

    "This can only be done by the logged in user.", httpMethod = "PUT")
 @ApiResponses(Array(
 new ApiResponse(code = 400, message = "Invalid username supplied"),
 new ApiResponse(code = 404, message = "User not found")))
 @ApiImplicitParams(Array(
 new ApiImplicitParam (name = "username", value = "name that need to be 
 updated", required = true, dataType = "String", paramType = "path"),
 new ApiImplicitParam(name = "body", value = "Updated user object",
 required = true, dataType = "models.User", paramType = "body")))
 def updateUser(username: String) = Action { implicit request =>
 request.body.asJson match {
 case Some(e) => {
 val user = Json.mapper.readValue(e.toString, classOf[User])
 userData.addUser(user)
 JsonResponse(user)
 }
 case None => JsonResponse(new value.ApiResponse(400, "Invalid input"))
 }
 }
  5. @ApiResponses(
 value = {
 @ApiResponse(code = 200, message = "leaking

    resource available"),
 @ApiResponse(code = 404, message = "cannot find leaking resource"),
 }
 )
 public Status getLeakingInformation() {
 return new Status(200);
 }
  6. @ApiResponses(
 value = {
 @ApiResponse(code = 200, message = "leaking

    resource available"),
 @ApiResponse(code = 404, message = "cannot find leaking resource"),
 }
 )
 public Status getLeakingInformation() {
 return new Status(451);
 }
  7. {
 "_links" : {
 "breweries" : {
 "href" : "http://localhost:8888/breweries"


    },
 "beers" : {
 "href" : "http://localhost:8888/beers"
 },
 "profile" : {
 "href" : "http://localhost:8888/profile"
 }
 }
 } $ curl 'http://localhost:8080/' -i
  8. @Test
 public void index() throws Exception {
 mockMvc.perform(RestDocumentationRequestBuilders.get(„/")) .andExpect(MockMvcResultMatchers.status().isOk())
 .andDo(

    document("index-page", links(
 linkWithRel("breweries").
 description("The <<resources-breweries,breweries resource>>“),
 linkWithRel("beers").description("The <<resources-beers,Beers resource>>"),
 linkWithRel("profile").description("The ALPS profile for the service")
 ), responseFields( fieldWithPath("_links")
 .description("<<resources-index-links,Links>> to other resources“) )
 ));
 }
  9. @Test
 public void index() throws Exception {
 mockMvc.perform(RestDocumentationRequestBuilders.get(„/")) .andExpect(MockMvcResultMatchers.status().isOk())
 .andDo(

    document("index-page", links(
 linkWithRel("breweries").
 description("The <<resources-breweries,breweries resource>>“),
 linkWithRel("beers").description("The <<resources-beers,Beers resource>>"),
 linkWithRel("profile").description("The ALPS profile for the service")
 ), responseFields( fieldWithPath("_links")
 .description("<<resources-index-links,Links>> to other resources“) )
 ));
 }
  10. @Test
 public void index() throws Exception {
 mockMvc.perform(RestDocumentationRequestBuilders.get(„/")) .andExpect(MockMvcResultMatchers.status().isOk())
 .andDo(

    document("index-page", links(
 linkWithRel("breweries").
 description("The <<resources-breweries,breweries resource>>“),
 linkWithRel("beers").description("The <<resources-beers,Beers resource>>"),
 linkWithRel("profile").description("The ALPS profile for the service")
 ), responseFields( fieldWithPath("_links")
 .description("<<resources-index-links,Links>> to other resources“) )
 ));
 }