SoFunction
Updated on 2025-03-04

Springboot access backend static resources 404 issue

springboot access backend static resources 404

404 error occurred while accessing background static resources using springboot

reason

The access path configured in the foreground may contain the path at the same level of the public resource class, for example:

When accessing acquired resources, "public/" and "static/" are added, but this level of path is not required to be loaded when accessing the instance. It is solved by using WebMvcConfigurer

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        ("/**").allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowCredentials(true).maxAge(3600);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //Open static, templates, public directory, but the corresponding prefix is ​​required when requesting, for example, I access the resource under static/static/xxxx/        ("/static/**","/templates/**","/public/**")
                .addResourceLocations("classpath:/static/","classpath:/templates/","classpath:/public/");

    }

}

Secondly, the path you wrote does not belong to the default access path

spring:
  mvc:
    static-path-pattern: /res/** # Static resource access prefix is ​​res
---
spring:
  resources:
    static-locations: [classpath:/res/] #The static resources in the res folder of the classpath can be accessed

Mybatis default camel name to underscore

By default, MyBatis converts the attribute name of the entity class to lowercase, converts camel nomenclature to underscore nomenclature, and then matches the column names of the database table.

If you have a nameUserThe entity class, which has a name calleduserNameThe property of MyBatis will map it to the database table by defaultuser_nameList

@IdIs an annotation in MyBatis that identifies properties in entity classes as primary keys for tables.

In MyBatis, if you want to use@IdAnnotations to identify primary key attributes, and also need to be used@GeneratedValueAnnotation to specify how primary keys are generated.

public class User {
    @Id
    @GeneratedValue(strategy = )
    private Integer id;
    private String username;
    private String password;

    // Omit the getter and setter methods}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.