1.SpringBoot json support
Creating 1.1 entity bean Car
Lombok use:
1, introducing dependencies
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
2, install the plug
carry on:
3, for use in an entity bean
@Data //get 、set
@AllArgsConstructor // all parameters have parameter constructor
@NoArgsConstructor // no-argument constructor
package com.wf.po; lombok.AllArgsConstructor import; import lombok.Data; import lombok.NoArgsConstructor; @Data //get 、set toString @AllArgsConstructor // all parameters have parameter constructor @NoArgsConstructor // no-argument constructor public class Car { private Integer id; private String name; private Float price; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date createdate; }
4. Create Controller CarController
package com.wf.controller; import com.wf.po.Car; import org.springframework.cache.CacheManager; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/car") public class CarController { @RequestMapping("/findone") public Car findOneCar(){ Car car = new Car(1, "toyo", 1999.99F,new Date(),"13567890001"); return car; } }
Description : @RestController notes, equal to @Controller used in conjunction with @ResponseBody
@requestBody annotation used to handle than the default content-type application / content x-www-form-urlcoded encoding, for example: application / json or application / xml like.
Which is generally used to process application / json type
1.2, SpringBoot request transmission parameters
2.1. First class: the request path parameter passing
@RequestParam get query parameters. That url? Name = value this form
@PathVariable get the path parameters. I.e. url / {id} in this form
Application Code:
(1) Modify Controller CarController new reception parameters, the method returns a single object
package com.wf.springbootdemo.controller;
import com.wf.springbootdemo.po.Car;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/car")
public class CarController {
@RequestMapping("/findOne")
public Car Demo1(){
Car car=new Car(1,"小奔驰",1000,new Date());
return car;
}
// http://localhost:8080/car/findTwo/kkk?id=10
@RequestMapping("/findTwo/{name}")
public Car Demo2(@RequestParam(name = "id") Integer id, @PathVariable(name = "name") String name){
Car car=new Car();
car.setId(id);
car.setName(name);
car.setPrice(1999);
car.setCreatedate(new Date());
return car;
}
// @PathVariable占位符 @RequestParam问号后面的参数
//http://localhost:8080/car/findThree/10?name=%E5%A4%A7%E5%AE%9D%E9%A9%AC
@RequestMapping("/findThree/{id}")
public Car Demo3(@PathVariable(name = "id") Integer id, @RequestParam(name = "name") String name){
Car car=new Car();
car.setId(id);
car.setName(name);
car.setPrice(1999);
car.setCreatedate(new Date());
return car;
}
}
Test passed a single object parameter acquisition json
Request Address:
http://localhost:8080/car/findTwo/kkk?id=10或
http: // localhost:? 8080 / car / findThree / 10 name =% E5% A4% A7% E5% AE% 9D% E9% A9% AC ( note that the latter is not garbled, is converted into Chinese characters after utf-8 effect)
UTF-transcoder. 8
https://utf8.supfree.net/
1.3, SpringBoot static resources
(1) Spring Boot static resource mapping provides a default configuration
Spring Boot by default / ** All access maps to the following directory:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
Such as: New public, resources, static three directories under the resources directory, and pictures were placed a.jpg b.jpg c.jpg
Renderings show:
(2), custom static resource access
The first way:
1, configuration class
package com.wf.springbootdemo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; // custom configuration class @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // map the resources under a disk to the browser
All // E: \\ springboot \\ pic \\ access is mapped to / myPhoto / ** path
registry.addResourceHandler("/myPhoto/**").addResourceLocations("file:E:\\springboot\\pic\\"); } }
When accessed using this path: http: // localhost: 8080 / myPhoto / 1.jpg
The effect of presentation:
The second way:
First, we configure application.properties
web.upload-path=E:/springboot/pic spring.mvc.static-path-pattern=/** spring.resources.static-locations=classpath:/META-INF/resources/, classpath:/public/,classpath:/static/,file:${web.upload-path} note: web.upload-path: This custom attribute belongs, specifies a path, attention / To end; spring.mvc.static-path-pattern = / **: indicates that all access is through the static resource path; spring.resources.static-locations: Here configuring a static resource path, said earlier configuration here is to cover the default configuration, so it is necessary to also add a default or static, public, etc. These paths will not be used as a static resource path, in this most end of the file: $ {web.upload-path} to add all of the file: is because the specified path is a specific disk, the other refers to the classpath system environment variables.
For example, in D: / springboot / pic / picture there is a 8.png
In the browser, enter: http: // localhost: 8080 / 8.png to visit
1.4, WebJars
WebJars practice:
1, create a META-INF / resources / webjars / demo / 0.0.1 directory under src / main / resouces path, and in order to demonstrate the effect, a copy of a picture this directory.
2.
Write a simple html page, put in src / main / resources / static (of course, can be placed directly webjar down, only to later add a mapping relation), reads as follows
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <Title> access static resources </ title> </head> <body> <-! Webjars / demo / 0.0.1 / 8.png this path corresponds to the path you just created -> <Img src = "webjars / demo / 0.0.1 / 8.png" alt = "address mapping"> </body> </html>
3, write configuration class, add a resource mapping relationship. In fact, you can not write, because Section 4 also have said, springboot four default resource path which contains the / META-INF / resources / a
package com.wf.springbootdemo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; // custom configuration class @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Configure the mapping relationship (resource file path path path of joining the front and back is the corresponding) registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
4. Access to the corresponding html