一.springboot官方demo开发
首先,引入maven的pom.xml依赖和父类
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
然后,编写javaclass类代码:
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home(){
return "Hello World!";
}
public static void main(String[] args) throws Exception{
SpringApplication.run(SampleController.class,args);
}
}
最后,打开浏览器,输入:http://localhost:8080/,可现实Hello World!
说明:springboot默认端口为8080
二.需求携带参数的get请求两种开发方式
1.第一种实现方式 url:key=value&key=calue
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
public Map<String,Integer> getList(@RequestParam Integer start,@RequestParam Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
2.第二种需要携带参数访问的get请求,url:ip:port/get/with/param/10/20
@RequestMapping(value = "/get/with/param/{start}/{end}")
public Map myGetList(@PathVariable Integer start,@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
三.SpringBoot集成SwaggerUI
1.配置pom.xml文件
<!--提取的swagger公共属性,因为版本号是一样的。其中标签<swagger.version>是自定义的,自己命名的-->
<properties>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
2.编写配置文件
package com.course.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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //这个注解表示这是一个配置文件
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("我的接口文档")
.contact(new Contact("wangyang0718","","834716115@qq.com"))
.description("这是我的swaggerui生成的接口文档")
.version("1.0.0.0")
.build();
}
}
3.在引入的方法中添加如下相关注解
首先,类上面要加注解:
@Api(value = "/",description = "这是我全部的get方法")
然后,所以方法上面要加注解:
@ApiOperation(value = "通过这个方法可以获取到Cookies",httpMethod = "GET")
4.访问地址路径:http://localhost:8888/swagger-ui.html