<?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">
<parent>
<artifactId>Chapter</artifactId>
<groupId>com.course.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Chapter10</artifactId>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
</dependencies>
</project>
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);
}
}
直接启动,然后访问127.0.0.1:8080 就可以显示出 Hello World!
一、开发一个返回cookies信息的get接口,首先创建一个Application类,这个类是一个入口类,还要在resources文件下,新建一个配置必须写这样的名字,才会被springboot识别到application.properties,端口号配置规定是这么写的,server.port=${port:8899},springboot会自动加载这个配置。
/**这标签意味这个给他托管了*/
@SpringBootApplication
/**扫描哪个包下面的类托管给我,注意目录要用到的类必须在被扫描的目录下*/
@ComponentScan("com.course")
public class Application {
public static void main(String[] args) {
/**固定写法*/
SpringApplication.run(Application.class,args);
}
}
/**这个注解是告诉application类的,我是需要被扫描的类*/
@RestController
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
/**在每一个方法上加上这个注解,value的值是描述,怎么样访问,后面swaggerui 生成接口文档用*/
@ApiOperation(value = "通过这个方法可以获取到cookies",httpMethod = "GET")
public String getCookies(HttpServletResponse response){
/**HttpServletRequest 装请求信息的类*
*HttpServletResponse 装响应信息的类
*/
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你获得了cookies信息成功";
}
}
server.port=${port:7788}
二、 一个要求携带cookies信息访问的get接口开发
/**
* 要求客户端携带cookies访问
* 这是一个需要携带cookies信息才能访问的get请求
*/
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
@ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "GET")
public String getWithCookies(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if(Objects.isNull(cookies)){
return "你必须携带cookies信息来";
}
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") &&
cookie.getValue().equals("true")){
return "这是一个需要携带cookies信息才能访问的get请求!";
}
}
return "你必须携带cookies信息来";
}
三、携带参数的get请求开发两种方式
一种是访问路径 http://127.0.0.1:7788/get/with/param?start=10&end=20
/**开发一个需要携带参数才能访问的get请求
* 第一种实现方式url:key=value&key=value
* 主要学习注解@RequestParam
*/
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
/**在每一个方法上加上这个注解,value的值是描述,怎么样访问,后面用swaggerui生成接口文档用*/
@ApiOperation(value = "需要携带参数才能访问的get请求第一种实现",httpMethod = "GET")
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
Map<String,Integer> myList= new HashMap<>();
myList.put("方便面",3);
myList.put("火腿肠",2);
myList.put("榨菜",1);
return myList;
}
一种是访问路劲 http://127.0.0.1:7788/get/with/param/10/20
/**第二种需要携带参数访问的get请求
* 路径的区别,url:ip:port/get/with/param/10/20
* 主要学习注解@PathVariable
* 写路径的方式/get/with/param/{start}/{end}
*/
@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
/**在每一个方法上加上这个注解,value的值是描述,怎么样访问,后面用swaggerui生成接口文档用*/
@ApiOperation(value = "需要携带参数才能访问的get请求第二种实现",httpMethod = "GET")
public Map myGetList(@PathVariable Integer start,
@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("方便面",1);
myList.put("火腿肠",2);
myList.put("榨菜",3);
return myList;
}