springboot-mybatis配置swagger

117 阅读1分钟

1.在原有的springboot-mybatis上配置

2.导入swagger包

                <!-- swagger2包 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>

3.使用注解配置swagger。

@ApiOperation(value = "获取用户列表")\

@ApiOperation(value = "用户登录", notes = "这是登录测试")\

。。。。。

package com.mwk.myproject.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mwk.myproject.entity.User;
import com.mwk.myproject.service.UserService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * springboot-mybatis配置练习
 * 
 * @author 凯凯
 *
 */
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation(value = "获取用户列表")
	@RequestMapping(value = "findAll", method = RequestMethod.GET)
	public Map<String, Object> findAll() {
		Map<String, Object> map = new HashMap<>();
		List<User> ulist = userService.findAll();
		map.put("result", ulist);
		map.put("status", 200);
		map.put("message", "请求成功");
		return map;

	}

	@ApiOperation(value = "用户登录", notes = "这是登录测试")
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public Map<String, Object> login(@ApiParam(value = "账号") @RequestParam("name") String name,
			@ApiParam(value = "年龄,这里假设为密码") @RequestParam("age") Integer age) {
		Map<String, Object> map = new HashMap<>();

		if (name == null || age == null) {
			map.put("status", 203);
			map.put("message", "账号或密码为空");
			return map;
		}

		User u = userService.findUserByName(name);
		if (u != null) {
			if (u.getAge() == age) {
				map.put("status", 200);
				map.put("message", "登录成功");
			} else {
				map.put("status", 201);
				map.put("message", "密码错误");
			}
		} else {
			map.put("status", 202);
			map.put("message", "账号错误");
		}
		return map;

	}
}

4.配置swagger.java

\

package com.mwk.myproject;

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;


@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mwk.myproject"))
                .paths(PathSelectors.any())
                .build();
    }

    @SuppressWarnings("deprecation")
	private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot-Mybatis-Swagger练习")
                .contact("闵渭凯")
                .version("1.0")
                .build();
    }

}

5.打开浏览器运行

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

出现这样就成功了

\

Demo源码下载\