Spring Boot and Swagger in JAVA

boneix 2019-01-08

SpringBootandSwaggerinJAVA

Followedthisdocument

https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/

Findsomeexamples

https://github.com/springfox/springfox-demos

Gitclonethebaseprojecttolocal

>gitclonehttps://github.com/springfox/springfox

Buildandgeneratepackagetolocalmaven

>./gradlewcleanbuildpublishToMavenLocal-i

ThenIcanimportandchecktheexampleprojectasfollow

springfox-demos/springfox-integration-webflux

Usuallythegenerateddocumentswillbehere

http://localhost:8080/v2/api-docs

TheSwaggerUIwillbehere

http://localhost:8080/swagger-ui.html#/

Moreannotationexample

https://github.com/swagger-api/swagger-core/wiki/annotations#quick-annotation-overview

HereismyworkingSwaggerConfiguration.

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.9.2</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.9.2</version>

</dependency>

SwaggerConfiguration

packagecom.kikokang.connector.netsuite.config;

importstaticcom.google.common.base.Predicates.not;

importstaticspringfox.documentation.builders.PathSelectors.regex;

importstaticspringfox.documentation.builders.RequestHandlerSelectors.basePackage;

importjava.util.Collections;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importspringfox.documentation.service.ApiInfo;

importspringfox.documentation.spi.DocumentationType;

importspringfox.documentation.spring.web.plugins.Docket;

importspringfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration

@EnableSwagger2

publicclassSwaggerConfig{

@Bean

publicDocketapi(){

returnnewDocket(DocumentationType.SWAGGER_2).select().apis(not(basePackage("org.springframework.boot")))

.paths(not(regex("/error"))).build().apiInfo(apiInfo());

}

privateApiInfoapiInfo(){

returnnewApiInfo("NetsuiteConnectorRESTfulAPI",null,"APIV1",null,null,null,null,

Collections.emptyList());

}

}

IntheController

@Api(value="/api/customer/")

@ApiOperation(value="GetCustomer",response=com.kikokang.connector.netsuite.model.CustomerDTO.class)

@GetMapping(path="/v1/{id}",produces=MediaTypes.JSON_UTF_8)

publicMono<CustomerDTO>get(@ApiParam(defaultValue="1525",required=true)@PathVariable("id")Stringid,

@ApiParam(defaultValue=“xxxx1",required=true)@RequestHeader(name="account",required=false)Stringaccount,

@ApiParam(defaultValue=“xxxx2",required=true)@RequestHeader(name="tbaConsumerKey",required=false)StringtbaConsumerKey,

@ApiParam(defaultValue=“xxxx3",required=true)@RequestHeader(name="tbaConsumerSecret",required=false)StringtbaConsumerSecret,

@ApiParam(defaultValue=“xxxx4",required=true)@RequestHeader(name="tbaTokenId",required=false)StringtbaTokenId,

@ApiParam(defaultValue=“xxxx5",required=true)@RequestHeader(name="tbaTokenSecret",required=false)StringtbaTokenSecret){

LOGGER.info("account:"+account+"tbaConsumerKey:"+tbaConsumerKey+"tbaConsumerSecret:"

+tbaConsumerSecret+"tbaTokenId:"+tbaTokenId+"tbaTokenSecret:"+tbaTokenSecret);

if(!WebUtil.validateHeader(account,tbaConsumerKey,tbaConsumerSecret,tbaTokenId,tbaTokenSecret)){

returnMono.error(newHeaderMissingException("TokenandConsumerKeymissinginHeader"));

}

HeaderDOheader=WebUtil.getHeaderDO(account,tbaConsumerKey,tbaConsumerSecret,tbaTokenId,tbaTokenSecret);

try{

Customercustomer=customerService.getByID(id,header);

CustomerDTOreturnDto=CustomerTransform.convertToCustomerDTO(customer);

returnMono.just(returnDto);

}catch(ServiceExceptione){

LOGGER.error("ServiceException",e);

returnMono.error(e);

}

}

References:

https://github.com/springframeworkguru/springboot_swagger_example

https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/

https://swagger.io/

相关推荐

莫问前程 / 0评论 2020-01-07