Knowledge changes fate, stroke code makes me happy, 2020 continue to walk in the open source community click “like” to see, form a habit to give me a Star, click to understand the componentialized interface services based on SpringBoot landing solution

An overview of the

When we write separate projects before and after, the front-end projects typically require static resources (Image, CSS, JavaScript…). To render the interface, and if we provided using external use depend on the way, our static resource files should also be in the packaging file, so you can more convenient to provide our function, in my open source distributed logging framework provides a management interface function in minbox – logging, implementation is used in this way, Store static resources and compiled HTML pages in the minbox-logging-admin-UI dependency. Let’s see how this works.

Recommended reading

  • Springboot2.x tutorial summary

Learn about Resources Static Locations

Before we pack up static resources, let’s take a look at the spring.resources.static-Locations configuration default provided by SpringBoot, which is used to configure ResourceHandler, When the project starts, the list of configuration values for this parameter is mapped as a directly accessible static directory, which gives us direct access to the static resource content we need.

Spring. The resources. The static – locations configuration in the org. Springframework. Boot. Autoconfigure. Web. The ResourceProperties configuration in the class, The default value is the value of the static constant CLASSPATH_RESOURCE_LOCATIONS in this class, as shown below:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/"."classpath:/resources/"."classpath:/static/"."classpath:/public/" };

/** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
Copy the code

Resources in the classpath:/ meta-INF /resources/ directory can be accessed directly through the default mapping binding. This allows us to store static resource dependencies in the meta-INF /resources directory.

Resource pack

Using Maven to build a normal project, add the resource directory configuration to the pop.xml file, and copy all files from SRC /main/resources to meta-INF /resources during compilation, as shown below:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <targetPath>META-INF/resources</targetPath>
    </resource>
  </resources>
</build>
Copy the code

To verify resource access, we store an image named head.jpg in the SRC /main/resources directory.

For our local demonstration, we installed the Maven project into a local repository using the MVN install command, so that it can be easily made available to other projects.

Use WebJars dependencies

Let’s create a SpringBoot project and add the following dependencies to the project’s POM.xml file:

<dependencies>
  <! Static resource access mapping binding requires web dependency -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>webjars-sample</artifactId>
    <version>1.0 the SNAPSHOT</version>
  </dependency>
</dependencies>
Copy the code

Since we previously installed the static resource project into the local repository using the MVN install command, we can use dependencies.

Using the IDEA tool, you can view the resource files in the Webjars-sample dependency, as shown below:

Because SpringBoot provides the default value of the spring.resources.static-locations parameter, classpath:/ meta-INF /resources directory as a static resource mapping, we can directly access the head.jpg file.

Run SpringBoot project, by visiting http://localhost:8080/head.jpg, the effect is as follows:

Static resource access prefix

We use IP :port/head.jpg to access static resources. SpringBoot also provides a configuration for spring.mvc. Static-path-pattern, which is used to prefix static resources. The default value is /**. If you need to change it, you can directly assign it to the application.yml file.

The application.yml configuration file, as follows:

spring:
  application:
    name: example
  mvc:
    static-path-pattern: /static/**
Copy the code

. We have modified the spring MVC. Static path – the pattern configuration has a value of static / * *, after when we restart the project need through http://localhost:8080/static/head.jpg can access to the resource.

conclusion

If you have resources that you don’t want to change to make it easier for users to integrate, you can encapsulate your Webjars in this way. Simply add dependency references to access static resources, or you can package static HTML pages in this way.

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect