SpringBoot 基础知识

本贴最后更新于 1298 天前,其中的信息可能已经沧海桑田

SpringBoot.jpg

创建 SpringBoot2.x

地址

⽬录说明

src/main/java: 存放代码
src/main/resources
static: 存放静态⽂件,⽐如 css、js、image (访问⽅式 http://localhost:8080/js/main.js)
templates: 存放静态⻚⾯ jsp、html、tpl
confifig: 存放配置⽂件,application.properties
resources:

同个⽂件的加载顺序

从 META/resources > resources > static > public ⾥⾯找是否存在相应的资源,如果有则直接返回,不在默认加载的⽬录,则找不到

默认配置

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

官网地址

Application 启动类位置三种形式

  • 当启动类和 controller 在同⼀类中时,在该类上添加注解 @Controller 即可;
  • 当启动类和 controller 分开时,启动类要放在根⽬录下,启动类上只需要注解 @SpringBootApplication;
  • 当启动类和 controller 分开时,如果启动类在⾮根⽬录下,需要在启动类中增加注解 @ComponentScan,并配置需要扫描的包名,如(basePackages = )
    • @ComponentScan(basePackages ={"net.xdclass.controller","net.xdclass.service"})

HTTP 接口请求

  • restful 协议
@RequestMapping(path = "/{参数1}/{参数2}", method = RequestMethod.GET)
public Object findUser(@PathVariable("city_id") String cityId,@PathVariable("user_id") String userId ){ }
  • GetMapping
@GetMapping(value="/v1/page_user")
public Object pageUser(int from, int size){ }
  • 默认值,是否必须的参数
@GetMapping(value="/v1/page_user2")
public Object pageUserV2(@RequestParam(defaultValue="0",name="page") int from, int size){ }
  • bean 对象传参
@RequestMapping("/v1/save_user")
public Object saveUser(@RequestBody User user){ }
  • 获取 http 头信息
@GetMapping("/v1/get_header")
public Object getHeader(@RequestHeader("access_token") String accessToken, String id){ }
  • ServletRequest 获取参数
@GetMapping("/v1/test_request")
public Object testRequest(HttpServletRequest request){
    params.clear();
    String id = request.getParameter("id");
    params.put("id", id);
    return params;
}
  • PostMapping
@PostMapping("/v1/login")
public Object login(String id, String pwd){ }
  • PutMapping
@PutMapping("/v1/put")
public Object put(String id){ }
  • DeleteMapping
@DeleteMapping("/v1/del")
public Object del(String id){ }

jackson 处理相关自动

  • 指定字段不返回:
    @JsonIgnore
    
  • 指定日期格式:
    @JsonFormat( pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8" )
    
  • 空字段不返回:
    @JsonInclude( Include.NON_NUll )
    
  • 指定别名:
    @JsonProperty
    

Dev-tool 热部署

官方文档

核心依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

不被热部署的文件

/META-INF/maven、/META-INF/resources、/resources、/static、/public、/templates

指定文件不进行热部署

spring.devtools.restart.exclude=static/,public/

手工触发重启

spring.devtools.restart.trigger-file=trigger.txt

配置文件

application.properties

server.port=8090
server.session-timeout=30
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8

application.yml

server: 
port: 8090
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8

官方参考

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    940 引用 • 1458 回帖 • 160 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...