swagger的由来
- 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,
而且前端技术和后端技术在各自的道路上越走越远。 - 前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,
swagger就是一款让你更好的书写API文档的框架。
其他API文档工具
- confluence
- README.md
- rap——API文档工具,阿里开发,rap2官网
- swagger——API文档框架,swagger官网
实例
- 使用Spring DATA JAP原生方法
- 解析方法名创建查询的特性
- 通过注解和JPQL语句创建查询等综合操作,完成对SysUser类中的各种操作
使用方法(选了一种适度封装之后的)
- 在pom.xml中引入依赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
- 在应用主类中增加@EnableSwagger2Doc注解
@SpringBootApplication
@EnableSwagger2Doc
public class Springboot2Swagger2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Swagger2Application.class, args);
}
}
- 默认情况下就能产生所有当前SpringMVC加载的请求映射文档
- 访问地址:hp://localhost:8080/swagger-ui.html
- 参数配置示例
swagger.enabled=true
swagger.title=spring-data-jpa module
swagger.description=Starter for swagger 2.x
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=RanRan Tao
swagger.contact.url=https://www.jianshu.com/u/822585e5c69a
swagger.contact.email=16422802@qq.com
swagger.base-package=com.example.book.controller
swagger.base-path=/**
swagger.exclude-path=/error, /ops/**
SysUser实体类
package com.example.book.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
@Data
public class SysUser implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String account;
private String password;
private String nickname;
private int fans;
public SysUser(String account, String password, String nickname, int fans) {
this.account = account;
this.password = password;
this.nickname = nickname;
this.fans = fans;
}
}
DAO接口 UserRepositry(正确的应该是UserRepository,代码里不小心写错了!)
package com.example.book.dao;
import com.example.book.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepositry extends JpaRepository<SysUser,Long> {
SysUser findByAccount(String account);
List<SysUser> findByFansBetween(Integer num1, Integer num2);
List<SysUser> findByNicknameLike(String nickname);
@Query("from SysUser u where u.nickname like CONCAT('%',:nickname,'%') order by u.fans desc ")
List<SysUser> findUsers(@Param("nickname") String nickname);
}
DAO测试类
package com.example.book.dao;
import com.example.book.entity.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositryTest {
@Resource
private UserRepositry userRepositry;
@Test
public void initData(){
SysUser[] users={
new SysUser("sdy@qq.com","111","素心若霞",247),
new SysUser("yjj@qq.com","222","与君绝",283),
new SysUser("ygkld@qq.com","333","一个快乐的退休俱乐部",1834),
new SysUser("jrys@qq.com","444","孑然一身",732),
new SysUser("yxyy@qq.com","555","诗心悠远",539)
};
for (int i=0;i<5;i++){
userRepositry.save(users[i]);
}
}
@Test
public void findOne() {
SysUser user = userRepositry.findById(42L).get();
System.out.println(user);
}
@Test
public void findByFansBetween() {
List<SysUser> sysUsers =userRepositry.findByFansBetween(200,300);
sysUsers.forEach(sysUser -> System.out.println(sysUser));
}
@Test
public void findByNicknameLike() {
List<SysUser> sysUsers=userRepositry.findByNicknameLike("心");
sysUsers.forEach(sysUser -> System.out.println(sysUser));
}
@Test
public void findByAccount() {
SysUser sysUser=userRepositry.findByAccount("sdy@qq.com");
System.out.println(sysUser);
}
@Test
public void findUsers() {
List<SysUser> users = userRepositry.findUsers("心");
users.forEach(sysUser -> System.out.println(sysUser)); }
}
Service接口
package com.example.book.service;
import com.example.book.entity.SysUser;
import java.util.List;
public interface UserService {
SysUser save(SysUser sysUser);
List<SysUser> getAll();
SysUser getUser(Long id);
List<SysUser> getUserfindByNicknameLike(String nickname);
SysUser getUserfindByAccount(String account);
List<SysUser> getUsersOrderByFans(String nickname);
}
Service层实现类
package com.example.book.service.Impl;
import com.example.book.dao.UserRepositry;
import com.example.book.entity.SysUser;
import com.example.book.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserRepositry userRepositry;
@Override
@Transactional
public SysUser save(SysUser sysUser) {
return userRepositry.save(sysUser);
}
@Override
public List<SysUser> getAll() {
return userRepositry.findAll();
}
@Override
public SysUser getUser(Long id) {
return userRepositry.findById(id).get();
}
@Override
public List<SysUser> getUserfindByNicknameLike(String nickname){
return userRepositry.findByNicknameLike(nickname);
}
@Override
public SysUser getUserfindByAccount(String account){
return userRepositry.findByAccount(account);
}
@Override
public List<SysUser> getUsersOrderByFans(String nickname) {
return userRepositry.findUsers(nickname);
}
}
Controller
package com.example.book.controller;
import com.example.book.entity.SysUser;
import com.example.book.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.persistence.GeneratedValue;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping(value = "/all")
public List<SysUser> getAll(){
return userService.getAll();
}
@GetMapping("/{id}")
public SysUser getUser(@PathVariable Long id) {
return userService.getUser(id);
}
@GetMapping(value = "/{nickname}")
public List<SysUser> getUserfindByNicknameLike(String nickname){
return userService.getUserfindByNicknameLike(nickname);
}
@GetMapping(value = "/{account}")
public SysUser getUserfindByAccount(String account){
return userService.getUserfindByAccount(account);
}
@GetMapping("/order")
public List<SysUser> getUsersOrderByFans(@RequestParam String nickname) {
return userService.getUsersOrderByFans(nickname);
}
}
使用swagger生成在线接口文档
-
所有方法的截图
image.png -
以getall方法获取所有信息的截图为例
image.png
网友评论