(ix) Generating document pages

Source: Internet
Author: User

EasyOpen provides a simple API document viewing page, similar to swagger, which generates document pages based on the annotation feature. The resulting document page can view parameters, result descriptions, or mock requests. It can be helpful for projects that are separated from front to back. The document interface is shown in the following figure:

The left side of the tree menu corresponds to the document name, click the tree to see the corresponding interface description. Click the Request button to initiate a request for testing. You can modify the values in the business parameters to test.

Here's how to use the documentation annotations.

The document page is closed by default and needs to be set in Apiconfig

Apiconfig.setshowdoc (TRUE);//is true to open the document page.

Next, set the document information for the Get product interface:

@Api (name = "Goods.get") public
goods getgoods (goodsparam param) {
    ...
    return goods;
}
set up document annotations

Add a @apidocmethod annotation on the interface method.

@Api (name = "Goods.get")
@ApiDocMethod (description= "Get Goods")//document annotations, description describes the public
goods getgoods for the interface ( Goodsparam param) {
    ...
    return goods;
}
setting parameter annotations

Enter the Goodsparam class, using @apidocfield annotations

public class Goodsparam {

    @ApiDocField (description = "Commodity name", required = true, example = "Iphonex")
    private String Goods_name;
    Omit get Set
}

@ApiDocField annotations are used to define field information, @ApiDocField annotations are defined as follows

/**
     * Field Description
     *
    /String description () default "";

    /**
     * Field name
     *
    /String name () default "";

    /**
     * Data type, int string float
     * @return
     *
    /DataType DataType () default datatype.string;

    /**
     * is required *
    /boolean required () default false;
set the return result annotation

Similarly, set @apidocfield in the goods class

public class Goods {
    @ApiDocField (description = "id")
    private Long ID;
    @ApiDocField (description = "Commodity name")
    private String goods_name;
    @ApiDocField (Description = "Prices", DataType = datatype.float)
    private BigDecimal price;
    Omit get Set
}

This has been set up, you can access the URL to preview the document page URL

The URL format for the document page is: Apiurl + "/doc". For example: Apiurl is HTTP://LOCALHOST:8080/API/V1, then the document page is: Http://localhost:8080/api/v1/doc list return

If the interface method returns a list, it is set up in the following way

    @Api (name = "Goods.list", Version = "2.0")
    @ApiDocMethod (description= "Get product List"
        , results={@ApiDocField ( description= "Commodity list", name= "list", Elementclass=goods.class)}
    ) public
    list<goods> Listgoods ( Goodsparam param) {

    }

Elementclass the element type in the list third-party class returns

If there is a PageInfo class that is in a third-party jar and cannot be modified, how do you write the corresponding document for its properties?

PageInfo class content is as follows:

Suppose this is the class in the jar and cannot be modified. But the document is to be generated public
class Pageinfo<t> {
    private int pageIndex;
    private int pageSize;
    Private long total;
    Omit get Set
}

We can explicitly declare the field information:

    @Api (name = "Goods.pageinfo", Version = "1.0")
    @ApiDocMethod (description= "Get product List"
            , results={@ApiDocField ( Name= "PageIndex", description= "page", datatype=datatype.int,example= "1"),
                    @ApiDocField (name= "PageSize", Description= "A few data per page", datatype=datatype.int,example= "ten"),
                    @ApiDocField (name= "Total", description= "a few data per page", "datatype=datatype.long,example="),
                    @ApiDocField (name= "Rows", description= "data", Datatype=datatype.array, Elementclass=goods.class),}
            ) public
    pageinfo<goods> PageInfo (goodsparam param) {

    }
Document model Reuse

If multiple interfaces return PageInfo, you need to copy and paste a large number of annotations, change a place need to change multiple interfaces, can not achieve the reuse effect. We can create a new Goodspagevo to inherit the PageInfo, and then write the document annotations on the head of the class so that the reuse effect can be achieved.

@ApiDocBean (Fields = {
    @ApiDocField (name= "PageIndex", description= "page", datatype=datatype.int,example= "1"),
    @ApiDocField (name= "PageSize", description= "a few data per page", datatype=datatype.int,example= "ten"),
    @ApiDocField ( Name= "Total", description= "a few data per page", datatype=datatype.long,example= ","
    @ApiDocField (name= "Rows", description= "Product List", Datatype=datatype.array,elementclass=goods.class), public
class Goodspagevo extends pageinfo<goods> {

}
    @Api (name = "Goods.pageinfo", Version = "2.0")
    @ApiDocMethod (description= "Get Product List", Resultclass=goodspagevo.class ) Public
    pageinfo<goods> Pageinfo2 (goodsparam param) {

    }

Use Resultclass=goodspagevo.class to specify the return result type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.