起步:SpringBoot

pom.xml

1 <parent>
2         <groupId>org.springframework.boot</groupId>
3         <artifactId>spring-boot-starter-parent</artifactId>
4         <version>2.2.0.RELEASE</version>
5         <relativePath/> <!-- lookup parent from repository -->
6 </parent>
maven依赖:
1
<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-test</artifactId> 9 <scope>test</scope> 10 <exclusions> 11 <exclusion> 12 <groupId>org.junit.vintage</groupId> 13 <artifactId>junit-vintage-engine</artifactId> 14 </exclusion> 15 </exclusions> 16 </dependency>


华为云镜像:

1 <mirror>
2     <id>huaweicloud</id>
3     <mirrorOf>*</mirrorOf>
4     <url>https://mirrors.huaweicloud.com/repository/maven/</url>
5 </mirror>

-基本web开发

1、导入依赖库
lombok的使用:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </dependency>

2.安装Lombok插件:plugins---->lombok

3.实体类中

@Data //get set tostring
@AllArgsConstructor //所有参数构造方法
@NoArgsConstructor  //空参构造方法
public class Car {
  private Integer id;
  private String name;
  private Float price;
//  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")               传入参数       
  @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")   传出参数 
  private Date createDate;
}

4.Controller:

请求参数两种类型:

  @RequestParam   获取查询参数。即url?name=value   这种形式

  @PathVariable   获取路径参数。即url/{id}        这种形式

@RestController             //相当于@Controller+@ResponseBody 类中所有方法都返回json数据
@RequestMapping("/car")
public class CarController {

    @RequestMapping("/demo")
    public String demo(){
        return "demo.html";
    }

-静态资源访问

1)、默认静态资源映射

Spring Boot 对静态资源映射提供了默认配置

Spring Boot 默认将 /** 所有访问映射到以下目录:

classpath:/static

classpath:/public

classpath:/resources

classpath:/META-INF/resources

2)、自定义静态资源访问

将静态资源路径设置到磁盘的基本个目录,上传文件时上传到磁盘中

方式1.使用配置类实现WebMvcConfigurer接口,重写addResourceHandlers(ResourceHandlerRegistry registry

 1 @Configurable
 2 public class WebMvcConfig implements WebMvcConfigurer{
 3     @Override
 4     public void addResourceHandlers(ResourceHandlerRegistry registry) {
 5         //配置映射关系
 6        //访问/images,则映射到d:\a\下 7         registry.addResourceHandler("/images/**").addResourceLocations("file:D:\\a\\");
 8 
11     }
12 }

在浏览器输入:http://localhost:8080/images/1.jpg即可访问。

方式2.配置application.properties

 

1 web.upload-path=D:/springboot/pic/
2 spring.mvc.static-path-pattern=/**
3 spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\
4 classpath:/static/,classpath:/public/,file:${web.upload-path}

web.upload-path:这个属于自定义的属性,指定了一个路径,注意要以/结尾;

spring.mvc.static-path-pattern=/**:表示所有的访问都经过静态资源路径;

spring.resources.static-locations:在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,

所以需要将默认的也加上否则staticpublic等这些路径将不能被当作静态资源路径,

在这个最末尾的file:${web.upload-path}之所有要加file:是因为指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量。

--WebJars

推荐使用Webjars的三大理由:

  • 将静态资源版本化,更利于升级和维护。
  • 剥离静态资源,提高编译速度和打包效率。
  • 实现资源共享,有利于统一前端开发。

使用步骤:

1.引入依赖:

 1        <dependency><!--Webjars版本定位工具(前端)-->
 2                 <groupId>org.webjars</groupId>
 3                 <artifactId>webjars-locator-core</artifactId>
 4             </dependency>
 5 
 6             <dependency><!--Jquery组件(前端)-->
 7                 <groupId>org.webjars</groupId>
 8                 <artifactId>jquery</artifactId>
 9                 <version>3.3.1</version>
10             </dependency>

2.访问静态资源

快速访问:http://localhost:8080/webjars/jquery/jquery.js (推荐)除去版本路径也可以访问得到

快速访问:http://localhost:8080/webjars/jquery/3.3.1/jquery.js

--SpringBoot属性配置

1.默认属性配置文件application.properties(可以修改后缀为.yml)

application.yml
server: port:
8888 servlet: context-path: /javaok

访问http://localhost:8888/javaok即可

2.自定义属性及读取

application.yml文件中,配置一些常量或者其他参数配置。读取的时候通过Spring@Value(${属性名})注解即可

 

application.yml
server:
  port: 8888
  servlet:
    context-path: /javaok
server_ip: 192.168.10.9999999999

 

@RestController
public class HelloController {

    @Value("${server_ip}")            //获取到自定义参数值server_ip
    private String server_ip;

    @GetMapping("/getMapping")
    public String getMapping(){
        return server_ip;
    }
}

3.实体类属性赋值

当属性参数变多的时候,我们习惯创建一个实体,用实体来统一接收赋值这些属性。

application.yml

user:
  name: www
  password: 8888888
  birthday: 1992.10.28
  mobile: 1234567890
  address: beijing
实体类:在实体类上增加注解@ConfigurationProperties,并指定prrfix前缀
@ConfigurationProperties(prefix
= "user") @Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private String password; private String birthday; private String mobile; private String address; }
Controller层:

EnableConfigurationProperties注解需要加在调用类上,或者加在启动类SpringbootSimpleApplication上也可以。


@RestController @EnableConfigurationProperties({User.
class,TestUser.class}) //多个实体类加载 public class UserController { @Autowired
User user;

@Autowired
TestUser testUser;

@GetMapping("/testUser")
public String test(){
return testUser.toString();
}

@GetMapping("/getUser")
public String getUser(){
return user.toString();
}

4.自定义配置文件

 

创建文件test.properties

testuser.name = "mike"
testuser.password = "123"
testuser.birthday = "1978.10.28"
实体类:

pring boot 1.5版本后@PropertySource注解就不能加载自定义的yml配置文件了

1.5版本后需要通过@PropertySource(classpath:test.properties)指定配置文件

 

@Configuration 注解包含@Component注解


@Configuration
@PropertySource("classpath:test.properties")
@ConfigurationProperties(prefix = "testuser")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestUser {
    private String name;
    private String password;
    private String birthday;

}

5.多环境配置文件

  可以使用多个yml来配置属性,将于环境无关的属性放置到application.yml文件里面;通过与配置文件相同的命名规范,创建application-{profile}.yml文件 存放不同环境特有的配置,例如 application-test.yml 存放测试环境特有的配置属性,application-prod.yml 存放生产环境特有的配置属性。

  通过这种形式来配置多个环境的属性文件,在application.yml文件里面spring.profiles.active=xxx来指定加载不同环境的配置,如果不指定,则默认只使用application.yml属性文件,不会加载其他的profiles的配置。

  

application.yml

spring:
   profiles:
    active: prod                //将调用application-prod.yml
application-test.yml

server:
  port: 8081
  servlet:
    context-path: /javaok1
 
 
application-prod.yml

server:
  port: 8082
  servlet:
    context-path: /javaok2
application-dev.yml

server:
  port: 8083
  servlet:
    context-path: /javaok3

--SpringBoot构建RESTful API

 

 1  Controller层:  
2 private List<User> listUser= Collections.synchronizedList(new ArrayList<User>()); 3 4 //查询所有 5 @GetMapping("/") 6 public List<User> getAllUser(){ 7 return listUser; 8 } 9 //获取指定id的user 10 @GetMapping("/{id}") 11 public User getUser(@PathVariable("id") Long id){ 12 for(User u:listUser){ 13 if(u.getId()==id){ 14 return user; 15 } 16 } 17 return null; 18 } 19 //插入 20 @PostMapping("/") 21 public String insert(User user){ 22 listUser.add(user); 23 return "success"; 24 } 25 //根据id修改 26 @PutMapping("/{id}") 27 public String update(@PathVariable("id") Long id,User user){ 28 for (User user2 : listUser) { 29 if(user2.getId()==id) { 30 user2.setName(user.getName()); 31 user2.setAge(user.getAge()); 32 } 33 } 34 return "success"; 35 } 36 //根据id删除 37 @DeleteMapping("/{id}") 38 public String delete(@PathVariable("id") Long id){ 39 listUser.remove(getUser(id)); 40 return "success"; 41 }

 

 

猜你喜欢

转载自www.cnblogs.com/crazy-lc/p/11795726.html
今日推荐