“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”


Related articles

Java with Notes: Java with Notes

preface

  • Hello, I don’t know what is the way of front and back end joint investigation in the company?

  • Tell me about myself

    • The last company used Yapi, and we tested the interface in PostMan for convenience during back-end development. And then you write it and you add it to Yapi, and you send the address to the front end, and the front end debuts it.
    • Now this is the use of Swagger, when writing a little trouble, but the time of the union is too cool crooked, what do not care, front-end play yourself! Ha ha ha ~
  • Of course, if your company doesn’t separate the front and back ends… When I didn’t say…

  • Since the work used, so certainly want to study, although this thing is very simple, but still can learn to learn!

SpringBoot integrates Swagger

  • Start by creating a New SpringBoot project that just needs the Web service.

  • New way can see my previous article, here is not redundant. Two ways to create a SpringBoot project

  • After the project is built, we import Swagger related configuration

    • <! -- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> < artifactId > springfox - swagger2 < / artifactId > < version > 2.9.2 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> < artifactId > springfox swagger - UI < / artifactId > < version > 2.9.2 < / version > < / dependency >Copy the code
  • New Configuration class for Swagger in the project

    • package com.dayu.dyswagger.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @configuration @enablesWagger2 public Class SwaggerConfig {}Copy the code
  • The general structure is as follows:

  • Start the project, visit: http://127.0.0.1:8080/swagger-ui.html#/

  • Congratulations, Swagger has been successfully integrated into the SpringBoot project!

  • As for basic-error-Controller, it’s because the SpringBoot project itself has an error return.

  • For example, enter a random route to see:

2. Customize Controller

  • New SwaggerTestController class

    • /** * @program: dyswagger * @description: Swagger test * @author: DaYu */ @RestController public class SwaggerTestController { @RequestMapping(value = "test-swagger",method = Requestmethod.get) public String dyTest(){return "Hello, big fish!" ; }}Copy the code
  • Start project access again and see

  • perfect

  • Since it is hydrology, you must write separately what can be finished in an article

  • In the next article, we will continue to explain the configuration and source code of SwaggerConfig.

  • Sorry guys, I had to fight to get to the event

  • Please call me the pen


The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah