This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

Configuration file – YAML

It is recommended to use YAML as a configuration file in Spring Boot development.

Basic grammar:

  • Key: value; There is a space between kV

  • Case sensitivity

  • Use indentation to indicate hierarchy

  • Indentation does not allow tabs, only Spaces

  • The amount of space indented does not matter, as long as elements of the same level are left aligned

  • ‘#’ indicates a comment

  • Strings do not need to be quoted. If you do, “” and “” indicate that the contents of the string will be escaped/not escaped

Data type:

The available data types are date\ Boolean \string\number\ NULL

  1. Single literal:

    k: v   # k: space v
    Copy the code
  2. Object: a set of key and value pairs, including map, hash, set, and Object

    • Common writing:

      k:
       k1: v1
       k2: v2
       k3: v3
      Copy the code
    • Inline writing

      k: {k1: v1.k2: v2.k3: v3}
      Copy the code
  3. Array: a group of values arranged in order. Array, list, queue

    • Common writing:

      k: 
       - v1
       - v2
       - v3
      Copy the code
    • Inline writing

      k: [v1.v2.v3]
      Copy the code

Before writing, I need to configure the prompt plugin in YAML:

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

Copy the code

If you do not need to package the plugin, you can set it in the plugin as follows:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>
Copy the code

Once the installation is complete, you need to start the project for it to take effect.

Example:

The Person class:

@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
Copy the code

Pet class:

@Data
public class Pet {
    private String name;
    private Double weight;
}
Copy the code

Create an application.yaml file in the recources resource directory, using yaml to represent the above attributes:

person:
  user-name: xbhog
  boss: false
  birth: 2021/ 7/27
  age: 18
  pet: {name: Will leave.weight: 23}
  interests: [Singing, dancing and playing games]
  animal:
    - jerry
    - mario
  score:
    english: 30
    math: 70
 # score: {English: 30,math: 70}
  salarys:
    - 277
    - 8999
    - 10000
  all-pets: # allPet has two k's (sick, health), each key contains a list
    sick: 
      - {name: tom}
      - {name: jerry.weight:47}
    health: [{name: mario.weight: 47}]
Copy the code

Create a controller to test whether our configuration file is working:

Because we mapped the properties in Person to the application.yaml file and added Person to the container.

@Component
@ConfigurationProperties(prefix = "person")
Copy the code

So let’s test it in MyConfig:

package com.xbhog.controller;

import com.xbhog.popj.Car;
import com.xbhog.popj.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired   // Automatically configure to find person in the container
    Person person;
    
    @RequestMapping("/person")
    public Person person(a){
        returnperson; }}Copy the code

The results are shown in the figure below:

Simple functional analysis of Web development

Static resource access problems

Static resources can only be stored in the following directory:

/static/, public/, resources/, meta-INF /resources

META-INF/resources/img.png returns 404. If you are interested, you can test it.

The method to access is: the current project root path / + static resource name (localhost:8080/xxx.img)

If our request route has the same name as the image, which one should Spring Boot request first?

We create a controller:

package com.xbhog.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class mycontro {
    @RequestMapping("/publicimg.png")
    public String demo(a){
        return "asss"; }}Copy the code

Make it the same as the image below the public folder:

The test results of opening the main program are as follows:

When we randomly request a nonexistent image, we are returned a 404.

So you can see from the above, when a request comes in, you go to the Controller and see if you can handle it. All requests that cannot be processed are handed over to the static resource handler. Static resource also not found respond 404 page

Change the default static resource path:

In the application.yaml file:

spring:
  mvc:
    static-path-pattern: /res/**
Copy the code

So when we access the resource, we have to add the res prefix.

However, you need to turn off · atic-path-pattern on welcome page support and custom Favicons. If enabled, index.html and favicon have no effect.

If the favicon Settings do not work, you can disable browser caching or restart idea.

The static resource folder can be set as follows:

Access localhost:8080 directly

Reference:

Silicon Valley Video

The end:

If you see here or just to help you, I hope you can point to follow or recommend, thank you;

If there are any errors, please point them out in the comments and the author sees them will be corrected.