1. Import Lombok dependencies in pom.xml in the created springboot project
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version>1.18.6</version> </dependency>
2. Install the Lombok plug-in
3. Create a package of the entity class at the same level as the main startup class, create the entity class in the package, and use Lombok on the entity class
package com.hxy.bean; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data //替代了getter、setter和toString方法 @AllArgsConstructor //创建所有参数的有参构造函数 @NoArgsConstructor //创建无参构造函数 public class Car { private Integer id; private String name; private double price; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createDate; }
4. Create a control layer package at the same level as the main startup class, and create a controller class
package com.hxy.controller; import com.offcn.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 //替代了@ResponseBody和@Controller注解 @RequestMapping("/car") public class CarController { @RequestMapping("/findone") public Car findOneCar(){ Car car = new Car(1, "toyo", 1999.99F,new Date(),"13567890001"); return car; } }
The above is the detailed content of SpringBoot basic web development demo method. For more information, please follow other related articles on the PHP Chinese website!