1.在建立的springboot專案中的pom.xml中導入Lombok的依賴
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version>1.18.6</version> </dependency>
2.安裝Lombok外掛
#3.在主啟動類別的同級建立實體類別的包,在套件中建立實體類,並在實體類別上使用Lombok
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.在主啟動類別的同級建立控制層的包,並建立controller類別
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; } }
以上是SpringBoot基本web開發demo的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!