SoFunction
Updated on 2025-04-05

Use SpringBoot Request Method and Accessing Static Pages

SpringBoot request method

ask

@RequestMapping(value = "/findUser",method = )
    public UserInfo findUser(){
        return new UserInfo("1","Zhang San","123","male","22");
    }

style

@GetMapping(value = "/rest/{id}/{name}")
    public Map findObject(@PathVariable(value = "id") String id, @PathVariable(value = "name") String name){
        ();
        ("id",id);
        ("name",name);
        return map;
    }

3. Pagination list

    @GetMapping(value = "/page")
    public Map pages(@RequestParam(name = "page",defaultValue = "1")Integer page,Integer limit){
        ();
        ("page",page);
        ("limit",limit);
        return map;
    }

4. Add data

    @PostMapping(value = "/add")
    public Map addUser(UserInfo ui){
        ();
        ("ui",ui);
        return map;
    }

form

    @GetMapping(value = "/findheader")
    public Map findHeader(@RequestHeader("token")String token,String id){
        ();
        ("token",token);
        ("id",id);
        return map;
    }

6. Find information

    @GetMapping(value = "/find")
    public Map findHeader(HttpServletRequest request){
        ();
        String id = ("id");
        ("id",id);
        return map;
    }

7. Log in

    @PostMapping(value = "/login")
    public Map login(String name ,String pwd){
        ();
        ("name",name);
        ("pwd",pwd);
        return map;
    }

8. Modify information

    @PutMapping(value = "/update")
    public Map update(String name){
        ();
        ("name",name);
        return map;
    }

9. Delete information

    @DeleteMapping(value = "/delete")
    public Map delete(String id){
        ();
        ("id",id);
        return map;
    }

Common frameworks Alibaba fastjson, Google gson

JavaBean serialization to Json, performance: Jackson > FastJson > Gson > Json-lib Same structure

Jackson, FastJson, and Gson library each have their own advantages and their own expertise.

Time to change space, time to change space

Jackson processing related automatic

  • The specified field does not return: @JsonIgnore
  • Specify date format: @JsonFormat (pattern="yyy-MM-dd hh:mm:ss" , locale="zh" , timezone="GMT+8" )
  • Empty field does not return: @JsonInclude(Include . NON_NULL)
  • Specify alias: @JsonProperty

SpringBoot directory file structure

1. Table of contents explanation

  • src/main/java: store code
  • src/main/resources
  • static: store static files, such as css, js, image, (access method http://localhost:8080/js/)
  • templates: store static pages jsp, html, tpl
  • config: store configuration files, application. properties

resources:

2. Introduce dependency Thymelesf

       <dependency>
           <groupId></groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>

If this dependency package is not attracted, the html file should be placed in the default loading folder, such as resources, static.public folders, before accessing.

3. Loading order of the same file, static resource files

Spring Boot will default to find yes from META/resources > resources > static > public if there is a corresponding resource

Source, if there is, return directly.

4. Default configuration

1) Official website address: https:///spring-boot/docs/current/reference/html/

boot-features -developing-web- applications . html#boot - features- spring-mvc-static-content

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

Spring Boot accesses static resources

Type directly in the browser localhost:8080/ Will visit static Static resources under folders

If you want to access ittemplates There are two ways to get static resources, but both needexistGo insideIntroduce Thymelesf dependencies.

1) Add the following configuration to determine the death

=classpath:/templates/
=.html

2) Add the following configurations to the flexibly accessed

-locations = classpath:classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/page/

Finally, access is performed in the Controller control layer

package ;

import ;
import ;

@Controller
public class TestController {

    @GetMapping("/finds")
    public String find(){
        return "a";
    }

    @GetMapping("/findss")
    public String finds(){
        return "";
    }
}

Summarize

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