“This is the 23rd 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

  • As the saying goes, sharpening the knife does not mistakenly cut wood workers, the front content is mainly to configure Swagger.
  • Today, we’re going to talk about the actual entity configuration that Swagger uses, and the interface configuration.
  • After all, what we’ve been saying isFront and rear end intermodulationSo how to make the front end understand our interface at a glance?

I. Entity configuration

1.1. Entity Configuration Cases

  • Student Entity class StudentResponse

    • @data @APIModel (value = "class ") public class StudentResponse {@apiModelProperty (value =" name ") private String name; @apiModelProperty (value = "age ") private int age; @apiModelProperty (value = "ApiModelProperty ") private String like; }Copy the code
  • Controller Changes the return value to StudentResponse

    • @RestController public class SwaggerTestController { @RequestMapping(value = "test-swagger",method = RequestMethod.GET) public StudentResponse dyTest(){ return new StudentResponse(); }}Copy the code
  • Restart test click model to view

  • That way, the front end makes everything clear about our return value!

  • No need to worry about the front end not understanding the return parameters

1.2. @apiModel () Annotation

  • Now that we know there is an annotation that provides documentation for our return value, what exactly is this annotation? What parameters can you set? Don’t worry. We’ll take our time.

  • As usual, go in and see, what are the parameters of this annotation? Let’s set them separately and see what happens

    • @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface ApiModel { String value() default ""; String description() default ""; Class<? > parent() default Void.class; String discriminator() default ""; Class<? >[] subTypes() default {}; String reference() default ""; }Copy the code
    • Explanation:

      • @Target({ElementType.TYPE}): This annotation is annotated on the class
      • @Retention(RetentionPolicy.RUNTIME): Takes effect at runtime
      • @InheritedWhen the @inherited annotation is added to A class A, when the class B inherits A, the @inherited annotation also carries this annotation.
      • About detailed annotations, children’s shoes are interested can look at my previous articles, very detailed: play Java annotations – yuan note, built-in annotations, principle and implementation of the custom annotation | Java annotations and reflection series (a)
  • Multi-parameter setting:

    • New Course CategoryCourseResponse
    • @apiModel (value = "class ") @data Public class CourseResponse {@apiModelProperty (value =" class ") private String course; }Copy the code
    • @ Data @ ApiModel (value = "students", the description = "this is such a detailed description of the information", the parent = CourseResponse. Class, subTypes. = CourseResponse class) Public class StudentResponse{@APIModelProperty (value = "name ") private String Name; @apiModelProperty (value = "age ") private int age; @apiModelProperty (value = "ApiModelProperty ") private String like; @ApiModelProperty() private CourseResponse perResponse; }Copy the code
  • Reboot again to see the effect:

  • The summary is as follows:

    • Value property: This property provides an alternate name for the class. If we do not set it, then by default, we will useclassClass name.
    • Description property: Provides a detailed description of the class
    • Parent property: This property describes some information about the parent of a class.
    • Discriminator attribute: This attribute is tricky to interpret because this class is primarily represented by assertions.
    • SubTypes attribute: For example, if we have a parent class at this pointAnimal. Also, for this parent class, we have subclasses of this class in our systemCat,Dog,PigAnd so on. If we specify a subclass that we want to use on our parent class via this property, then the generationSwaggerIs displayed automaticallyAnimalThis property, however, will show subclass-specific properties in the property field, which is not recommended here. Because it makes people think that the attributes that are unique to the subclass are also unique to the parent class.
  • aboutdiscriminatorThis property, let me give you a separate propertyassertionsArticle to fill the hole. Because I’m not so good right now.
  • It’s eleven o ‘clock. Fight again tomorrow!
  • Notice for tomorrow:@ApiModelProperty()Explanation.

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