Spring Boot簡單教程

1.spring boot的啓動類必須在其他類的包名之上
2.SpringBoot響應客戶端數據
    @RestController
    public class Hello {
    @GetMapping("/getuser")
        public User getUser() {
    User u = new User();
    u.setAge(100);
    u.setName("wanqing");
    u.setKey("imnu");
        return u;
        }
    }
    {"name":"wanqing","age":100,"key":"imnu"}
    
3.SpringBoot 使用devtools進行熱部署
    Maven.  
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    Gradle.
    dependencies {
        compile("org.springframework.boot:spring-boot-devtools")
    }
4.Spring Boot資源文件屬性配置
    配置屬性resource.properties
    com.imnu.asiainfo.name=wangqing
    com.imnu.asiainfo.age=12
    com.imnu.asiainfo.key=1231dfsf
    配置entity
    @Configuration
    @ConfigurationProperties(prefix="com.imnu.asiainfo")//前綴
    @PropertySource(value = "classpath:resource.properties")//引用文件
    public class User {
    private String name;
    private int age;
    private String key;
    }
    
    使用
    @Autowired
    private User user;
    @GetMapping("/getuser")
    public User getUser() {
    User u = new User();
    BeanUtils.copyProperties(user, u);
    return u;
    }
   
5.傳遞參數
    <單值傳入>
    @RequestMapping(value= "/user",method = RequestMethod.GET)
    public List<User> query(@RequestParam(name = "username",required=false,defaultValue="wangqing") String Lusername){
    System.out.println(Lusername);
    List<User> users = new ArrayList<>();
    return users;
    }
    <多值傳入>
   @RequestMapping(value= "/user",method = RequestMethod.GET)
public List<User> query(User user){
String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE); --將對象反射(使用jar org.apache.commons)
System.out.println(l);
List<User> users = new ArrayList<>();
return users;
}
<分頁>
@RequestMapping(value= "/user",method = RequestMethod.GET)
 public List<User> query(User user,@PageableDefault(page=2,size=10,sort="name,asc") Pageable pageable){
  String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
  System.out.println(l);
  return users;
 }
6.參數綁定
 @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
 public User getInfo(@PathVariable String id) {
  System.out.println("----------"+id);
  User u= new User();
  return u;
 }
7.JsonView
    使用接口來聲明多個視圖
    在值對象的get方法上指定視圖
    在Controller方法上指定視圖
    
    public class User {
 public interface UserSimpleView {};
 public interface UserDetailView extends UserSimpleView {};
 private String name;
 private int age;
 private String key;
      @JsonView(UserSimpleView.class)
 public String getName() {
  return name;
 }
      @JsonView(UserDetailView.class)
 public int getAge() {
  return age;
 }
 
 @RequestMapping(value= "/user",method = RequestMethod.GET)
 @JsonView(User.UserDetailView.class)
 public List<User> query(User user){
  String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
  System.out.println(l);
  List<User> users = new ArrayList<>();
  users.add(user);
  return users;
 }
 返回[{"name":"wangqing","age":12}]
 
 @RequestMapping(value= "/user",method = RequestMethod.GET)
 @JsonView(User.UserSimpleView.class)
 public List<User> query(User user){
  String l = ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE);
  System.out.println(l);
  List<User> users = new ArrayList<>();
  users.add(user);
  return users;
 }
 返回 [{"name":"wangqing"}]
 
8.post後臺數據綁定
 @PostMapping("/create")
 public User create(@RequestBody User user) {
 System.out.println(user.getAge());
 System.out.println(user.getName());
 System.out.println(user.getKey());
   return user;
  }
  
9.校驗參數 @Valid 的使用
  public class User {
  public interface UserSimpleView {};
  public interface UserDetailView extends UserSimpleView {};
 
  private String name;
  private int age;
  @NotBlank
  private String key;
 
  }
 
  @PostMapping("/create")
public User create(@Valid @RequestBody User user) {
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}   

BindingResult錯誤信息輸出
@PostMapping("/create")
public User create(@Valid @RequestBody User user ,BindingResult errors) {
if (errors.hasErrors()) {
errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
}
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getKey());
return user;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章