springBoot(1)整合Swagger

 參考文章 進行springBoot整合Swagger 記錄過程中所遇到的坑

1: 通過idea創建springBoot項目   此處只記錄下springBoot項目整合swagger   通過idea創建springBoot項目 暫不演示

2:在pom.xml 中添加swagger依賴

	<!-- swagger start -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.2.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.2.2</version>
		</dependency>
	<!-- swagger end -->

3.創建Swagger2配置類

package com.shan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author sc
 * @createTime 2019/5/20 15:49
 * @description
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改爲自己的包路徑
                .apis(RequestHandlerSelectors.basePackage("com.shan.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("sc測試api文檔標題")
                .description("sc測試api文檔描述")
                .version("1.0")
                .contact("sc")
                .build();
    }



}

 

 

 

4.Swagger使用的註解及其說明:

 

package com.shan.controller;

import com.shan.domain.User;
import com.shan.mapper.UserMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description = "用戶管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    @ApiOperation(value = "用戶列表", notes = "用戶查詢")
    @GetMapping("/findAll")
    @ResponseBody
    public List<User> finAll() {
        List<User> users = userMapper.queryUserList();
        System.out.println(users);
    /* List<User> users=new ArrayList<>();
     User user=new User();
      user.setName("tom");"123");
      users
      user.setPassword(.add(user)*/
        return users;
    }


    @ApiOperation(value = "根據id查用戶", notes = "根據id查指定用戶")
    @GetMapping("/finById/{id}")
    @ResponseBody
    public User finById(@PathVariable("id") Long id) {
        System.out.println(id);
        User user = new User();
        user.setId(id);
        user.setName("tom");
        user.setPassword("123");
        return user;
    }

    /**
     * (@ApiParam  註解必須配合 @RequestParam才能接受到參數
     * @param username
     * @return
     */
    @ApiOperation(value = "根據用戶名查用戶", notes = "根據用戶名查指定用戶")
    @GetMapping("/finByName")
    @ResponseBody
    public User finByName(@ApiParam(name="username",value = "用戶名",required = true) @RequestParam("username") String username) {
        System.out.println(username);
        User user = new User();
        user.setUsername(username);
        user.setPassword("123");
        return user;
    }


    @ApiOperation(value = "修改用戶", notes = "修改用戶")
    @PostMapping("/update")
    @ResponseBody
    public User update(@RequestBody(required = true) User user) {
        System.out.println(user.toString());
        User u = new User();
        u.setUsername(user.getUsername());
        u.setName(user.getName());
        return u;
    }



    /**
     * (@ApiParam  註解必須配合 @RequestParam才能接受到參數
     * @param username
     * @return
     */
    @ApiOperation(value = "根據參數查用戶", notes = "根據參數查指定用戶")
    @GetMapping("/finByParam")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value="用戶名",dataType="string", paramType = "query"),
            @ApiImplicitParam(name="id",value="用戶id",dataType="long", paramType = "query")})
    public User finByParam( String username ,Long id) {
        System.out.println(username);
        User user = new User();
        user.setUsername(username);
        user.setId(id);
        return user;
    }


}

 

 

參考文章

https://blog.csdn.net/wyb880501/article/details/79576784

https://blog.csdn.net/ysk_xh_521/article/details/80633141

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章