SwaggerUI操作演示

controller

@RestController
@Api(tags = "SaaS測試")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("保存用戶")
    @PostMapping("/save")
    @ResponseBody
    public User save(Integer id, String name, int age) {
        User user = new User();
        user.setId(id);
        user.setAge(age);
        user.setName(name);
        return userService.save(user);
    }
//
    @ApiOperation("根據ID查詢shanxi")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public List<User> findByAge(int age) {
        return userService.findByAge(age);

    }
//
    @ApiOperation("根據ID刪除用戶")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(int id) {
        userService.delete(id);
    }
}

dao


public interface UserDao extends JpaRepository<User,Integer> {



}

entity

import javax.persistence.*;

@Entity
@Table(name = "t_user")
public class User{
    private Integer id;
    private String name;
    private Integer age;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

service

public interface UserService {

    public User save(User user);
    public List<User> findByAge(int age);
    public void delete(int id);
}

serviceImpl

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User save(User user) {
        userDao.save(user);
        return user;
    }

    @Override
    public List<User> findByAge(int age) {
        User user=new User();
        user.setAge(age);
        Example example= Example.of(user);

        List list = userDao.findAll(example);
        return list;

//        return userDao.findAll();
    }

    @Override
    public void delete(int id) {
        userDao.deleteById(id);
    }
}

啓動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class GuofangApplication {

    public static void main(String[] args) {
        SpringApplication.run(GuofangApplication.class, args);
    }

}

task

@Component
public class ShanXiTask {

    @Autowired
    private UserService shanXiService;

    @Scheduled(cron = "0/10 * * * * ? ")
    private void cancelTimeOutOrder() {
        //TODO 待做事件  業務邏輯 生成mysql數據庫記錄
        System.out.println("這是一個定時任務!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        User sanXi=new User();
        sanXi.setAge(14);
        sanXi.setName("shanxi");
        shanXiService.save(sanXi);
    }
}


Swagger2Config

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //爲當前包下controller生成API文檔
                .apis(RequestHandlerSelectors.basePackage("com.shanxi.controller"))
                //爲有@Api註解的Controller生成API文檔
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //爲有@ApiOperation註解的方法生成API文檔
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerUI演示")
                .description("陝西國防大數據工坊")
                .contact("xhd")
                .version("1.0")
                .build();
    }
}

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