springboot项目如何创建?idea?02

创建如下图所示,箭头标记的东西,注意位置

在这里插入图片描述

MySwaggerConfigurer内容:

 package com.zhiyou100.springboot02.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;


@Configuration
@EnableSwagger2
public class MySwaggerConfigurer {

  @Bean // 把方法的返回值注入 ioc 容器
public Docket api() {

    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()                   //修改自己的项目包
            .apis(RequestHandlerSelectors.basePackage("com.zhiyou100.springboot02.controller"))
            .paths(PathSelectors.any())
            .build();
}

private ApiInfo apiInfo() {

    return new ApiInfoBuilder()
            .title("Springboot02 项目 Api 接口测试文档")
            .description("spring boot 02") //对项目的描述
            .version("1.0") //项目版本号
            .build();
    }
}

UserController内容:

package com.zhiyou100.springboot02.controller;

import com.zhiyou100.springboot02.entity.User;
import com.zhiyou100.springboot02.vo.ResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

//post  参数from表单 @RequestParam获取参数
//post  参数json   @RequestBody获取参数


//@RequestMapping(value = "/users",method = RequestMethod.GET )
@GetMapping("/users")  //查
public ResponseVo<String> getUser(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
    return  new ResponseVo<>(200,"","page:"+page+",size:"+size);
}

@PostMapping("/users")   //增
public  ResponseVo<User> addUser(@RequestBody User user){
    return new ResponseVo<>(200,"",user);
}

@PutMapping("/users")  //改
public ResponseVo<User> updateUser(@RequestBody User user){
    return new ResponseVo<>(200,"",user);
}

@DeleteMapping("/users/{id}")  //删
public  ResponseVo<Integer> deleteUser(@PathVariable("id") Integer id){
    return  new ResponseVo<>(200,"",id);
}

  /*  @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
public Integer getUserById(@PathVariable("id") Integer id){
    return id;
}*/

@GetMapping("/users/{id}")
public ResponseVo<Integer> getUserById(@PathVariable("id") Integer id){
    return new ResponseVo<>(200,"",id);
}

@GetMapping("/users/{id}/friends")
public ResponseVo<Integer> getFriendsByUserId(@PathVariable("id")Integer id,@RequestParam("page") Integer page,@RequestParam("size") Integer size ){
    return new ResponseVo<>(200,"",id);
  }
}

User内容:

package com.zhiyou100.springboot02.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data  //生成setter 和 getter 方法
@NoArgsConstructor //无参构造方法
@AllArgsConstructor  //有参构造方法
public class User {
    private  Integer id;
    private  String name;
    private  Integer age;

}

ResponseVo内容:

package com.zhiyou100.springboot02.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseVo<T> {
    /**
     * 请求结果的状态码,正常状况都返回 200/0 异常状况返回自定义的状态码
     */
    private  Integer code;

    //对 code 的解释说明
    private  String message;

    //返回给客户端的数据
    private  T data;
}

pom.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhiyou100</groupId>
    <artifactId>spring-boot-02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-02</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值