1、统一端口号
在resources文件夹下新建application.properties文件
添加以下一行,固定端口号为8888
server.port=${port:8888}
2、Springboot启动类
@SpringBootApplication标签代表启动类,main方法
添加@ComponentScan标签,代表扫描哪个包下的类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.example")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(com.example.demo.DemoApplication.class, args);
}
}
3、get方法
在类上加上@RestController标签
@RestController的作用等同于@Controller + @ResponseBody
(1)在response中添加cookie
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
public String getCookies(HttpServletResponse response){
//HttpServerletRequest装请求信息的类
//HttpServerletResponse装响应信息的类
Cookie cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜获得cookie成功";
}
(2)客户端request携带cookie进行访问
/*
* 要求客户端携带cookies访问
* 这是一个需要携带cookies信息才能访问的get请求
* 使用jmeter,设置好cookie,发送get请求成功
* */
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.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信息来";
}
(3)需要携带参数访问的get请求,两种方式
第一种方式:参数加@RequestParam标签,@RequestMapping(value = “uri” , method = RequestMethod.GET)
第二张方式:参数加@PathVariable标签,@RequestMapping(value = “uri/{参数名}/{参数名}”)
/*
* 开发一个需要携带参数才能访问的get请求
* 第一种实现方式 url:ip:port/get/with/param?key=value&key=value
* 我们来模拟获取商品列表
* */
@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;
}
/*
* 第二种需要携带参数访问的get请求的实现方式
* url:ip:port/get/with/param/10/20
* */
@RequestMapping(value = "/get/with/param/{start}/{end}")
public Map<String,Integer> getList2(@PathVariable Integer start,
@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
4、使用SpringBoot实现post接口
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/",description = "这是全部的post请求")
@RequestMapping("v1")
public class MyPostMethod {
//这个变量是用来装我们cookie信息的
private static Cookie cookie;
//用户登录成功获取到cookies,然后再访问其他接口获取到列表
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiOperation(value = "登录接口成功后获取到cookie",httpMethod = "POST")
public String login(HttpServletResponse response,
@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "password",required = true) String password){
if(userName.equals("zhangssan")&& password.equals("123456")){
cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜登录成功";
}
return "用户名或密码错误";
}
}