Spring boot notes -- boot outer layer and interface test

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG nhecelxt-163966357156) (D: \ 2021 \ MD \ springboot \ img \ springboot introduction. png)]

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-l4aklts9-163966357157) (the difference between img \ springboot and springmvc. png)]

1. Springboot startup class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootHelloApplication.class, args);
	}
}

The @ SpringBootApplication annotation in class is the core annotation of springboot. Its main function is to enable spring automatic configuration. Using this annotation is equivalent to adding the following three annotations:

@Configuration allows classes identified by other @ bean annotations to be added to the spring container, which is equivalent to the beans tag in the spring configuration file

@Enable autoconfiguration starts autoconfiguration

@ComponentScan will automatically scan the classes marked @ Component,@Service,@Repository,@Controller under the current package and sub package. This is equivalent to the context: component scan in the previous spring configuration file

1. The startup class is in the outermost layer

BLUF{X9`Z]$K5.png)

2. The startup class is not in the outermost layer

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-kop8auup-163966357158) (D: \ 2021 \ MD \ springboot \ img \ image-20211215160828321. PNG)]

3. Use of postman

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-inm2jo51-163966357160) (D: \ 2021 \ MD \ springboot \ img \ image-20211215164357756. PNG)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-p6uw92cc-163966357161) (C: \ users \ WYL \ appdata \ roaming \ typora user images \ image-20211215164453041. PNG)]

1. [the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ajoo6y4l-163966357162) (D: \ 2021 \ MD \ springboot \ img \ image-20211215164619275. PNG)]: interface request history

2. [the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zzpgmdb6-163966357163) (D: \ 2021 \ MD \ springboot \ img \ image-20211215164612193. PNG)]: interface collection

3. [the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-4wnd7muz-163966357164) (D: \ 2021 \ MD \ springboot \ img \ image-20211215164545938. PNG)]: select the place with the request method

4. Specific request path

5.params: common data parameters

6.Headers: header parameters

7. Form data: form submission

8.raw: json parameter

9.binary: binary parameter

4. Test the get request of the http interface

restful style

     * ```java
              * @re/**
          * Get user information, restful style
         */
        @RequestMapping(value = "/v1/{cid}/{uid}", method = RequestMethod.GET)
                         public Object findUser(@PathVariable("cid") String cid,
                                                @PathVariable("uid") String uid) {
                             map.put("cid", cid);
                             map.put("uid", uid);
                             return map;
                         }
                     ```

2.2 paging list

/**
* Paging list
* @return
*/
@GetMapping("/v1/page_user")
public Object pageUser(@RequestParam(name="page",defaultValue = "1") int curPage,int limit){...}
Save user information
aram user

@return

​       @RequestMapping("/v1/save_user")
​       public Object saveUser(User user){...}
/**Test http header information. Some requests will carry header information
 Carry a token (i.e. identity authentication) as an identification that demonstrates that you have permission to access the interface

   * Generally, the token is placed in the @ reqeustHeader
     eturn
             */
            @GetMapping("/v1/get_header")
            public Object testHeader(@RequestHeader("access_token") String accessToken,String id){...}
/**
        * Test request
        * @param request
        * @return
        */
       @GetMapping("/v1/test_request")
       public Object testRequest(HttpServletRequest request){...}

3. Test of post / put / delete request

    3.1/**
            * Test post request
            * @param name
            * @param pass
            * @return
            */
           @PostMapping("/v2/login")
           public Object login(String name,String pass){
               map.clear();
               map.put("name",name);
               map.put("pass",pass);
               return map;
           }
3.2/**
        * Test put requests, mostly for update operations
        */
       @PutMapping("/v2/put")
       public Object login(String id){
           map.clear();
           map.put("id",id);
           return map;
       }
3.3/**        * Delete according to id * @ param id * @ return        */       @DeleteMapping("/v2/del")       public Object delete(String id){           map.clear();           map.put("id",id);           return map;       }
  1. jackson related annotations (acting on entity class attributes)

    The specified field is not returned
    @JsonIgnore
    format date
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",locale = "zh",timezone = "GMT+8")
    Empty fields are not returned
    @JsonInclude(JsonInclude.Include.NON_NULL)
    assign an alias
    @JsonProperty("xxx")

5. Access to static files

There are 3 paths in the default resources below :  resources / static / public 


5.1 static The following static file access methods are located in static Static files under can be accessed directly static/img/xx.pngstatic/js/xx.js Access mode: localhost:8080/img/xx.png         localhost:8080/js/xx.js5.2 If other static directories are provided resources In addition to static Outside this static file,also resources and public These two directories, Three priorities resources   >   static  >   public5.3templates Files in cannot be accessed directly,Configuration required***Introduce dependency<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>***Then pass controller Do jump 5.4 For access to custom static paths resources Create below test Static folder, Put static files in it ,It cannot be accessed directly at this time,This sentence needs to be configured in the configuration file ,Configure access support for static paths ,You can not write by default,Default to support. However, if a new custom static path is added, it needs to be written out explicitly spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public///Add the static path of test and change it to spring resources. static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/

6. File upload
You can specify to upload to the current project or to other directories

Finally, the project will be marked jar package ,Need to introduce pom  <!--become involved jar Plug in configuration for       Otherwise, an error will be reported: an error will be reported after running:no main manifest attribute, in XXX.jar   -->    <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>    </plugin>If an error is reported during packaging: Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.567 s <<< FAILURE! - in ....Delete the test class of the current project and continue packaging

1.static, public and resources are folders that can be accessed directly

2.templates he needs to introduce the templates template dependency, and then access the page by adjusting the page in the background

Keywords: Java Spring Spring Boot

Added by aiwebs on Thu, 16 Dec 2021 18:20:39 +0200