关于SpringBoot的相关知识

Spring Boot 注解
@SpringBootApplication:
表示这个主程序类是一个Spring Boot应用,也是启动类,将注释的类托管给SpringBoot了

是@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

@RestController

就是@Controller+@ResponseBody组合,支持RESTful访问方式,返回结果都是json字符串


@SpringBootTest

具有Spring Boot支持的引导程序(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)
关键是自动导入测试需要的类。

https://www.jianshu.com/p/1e9dcb1d606c

resource文件下的文件必须 叫 application.properties 这样spring boot框架才能被正确加载

properties配置端口必须为 server.port = ${port:xx}

@RestController
public class GetMethod{
    @RequestMapping(value = "/getCookies" , method = RequestMethod.get , params = "huhansan")    
      public String getCookies(HttpServletResponse response){
            //方法括号里的不是参数,HttpServletRequest : 装请求信息的类
            //HttpServletResponse : 装响应信息的类
            Cookie cookie = new Cookie(name:"login" , value:"true");
            response.addCookie(cookie);
            return "恭喜获得cookie信息成功";
    
      }

      //携带上面的cookie访问接口
      @RequestMapping(value = "/get/with/cookies" , method = RequestMethod.get ) 
       public String getWithCookies(HttpServletRequest request){
             Cookie[] cookies = request.getCookies(); //cookies信息为数组
             //取出数组里的值
             if(Objects.isNull(cookies)){
                   return "你必须携带cookies信息来";}
             for(Cookie cookie :cookies){
                    if(cookie.getName().equals("login") && cookie.getValue().equals("true")){
                           return "success";
                    }
            }
      }    
    
}                

猜你喜欢

转载自www.cnblogs.com/xuzhongyin/p/12372619.html