常见注解
lombok
@Data -- getter和setter方法
@Getter -- getter方法
@Setter -- setter方法
@NoArgsConstructor -- 无参构造器
@AllArgsConstructor -- 全参构造器
junit
@Test 测试用例
spring
@Autowired 自动装配
@Qulifier 合格者
@Controller -- web层
@Service -- service层
@Component -- 一般
@Repository -- Dao层
上述四个注解没有本质区别,只是语义不同,建议在不同的层使用对应注解
springmvc
@RequestMapping
@GetMapping
@PostMapping
@RequestParam
@ResponseBody
@ResponseBody和@Controller 有一个 组合注解 @RestController
@PathVarables
其他
@JsonView:下面代码中校验是否输出
@NotBlank:非空
@Valid:下面代码中该注解与@NotBlank和BindingResult类联合使用
@RunWith(SpringRunner.class)
@SpringBootTest
@Before
@Test
import java.time.LocalDateTime;
import java.util.Date;
import org.hibernate.validator.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonView;
public class UserDto {
public interface simpleUser {
};
public interface detailUser extends simpleUser {
};
private String username;
@JsonView(simpleUser.class)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@JsonView(detailUser.class)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@JsonView(simpleUser.class)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JsonView(simpleUser.class)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@NotBlank(message = "密码不能为空")
private String password;
private Integer id;
@Past(message = "生日必须是过去的日期")
private Date birthday;
}
package com.per.web.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
import com.per.dto.UserDto;
@RestController
@RequestMapping("/user")
public class UserController {
@PostMapping("/create")
@JsonView(UserDto.simpleUser.class)
//@JsonView(UserDto.simpleUser.class)
public UserDto CreateUser(@Valid @RequestBody UserDto user,BindingResult errors) {
// 当被标记成@Valid,实体类@NotBlank,若传入的参数中被标记成非空的字段为空值,可以用BindingResult输出
if(errors.hasErrors()) {
errors.getAllErrors().stream().forEach(error->System.out.println(error.getDefaultMessage()));
}
user.setId(1);
System.out.println(user.getUsername()+'\t'+user.getPassword()+'\t'+user.getId()+'\t'+user.getBirthday());
return user;
}
}
package com.per.web.controllers;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenQuerySuccess() throws Exception {
String str = mockMvc.perform(MockMvcRequestBuilders.get("/user").contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)).andReturn().getResponse()
.getContentAsString();
System.out.println(str);
}
@Test
public void whenGetInfoSuccess() throws Exception {
String str = mockMvc
.perform(MockMvcRequestBuilders.get("/user/999067").contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("username").value("tom")).andReturn().getResponse()
.getContentAsString();
System.out.println(str);
}
@Test
public void whenGetInfoFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/a").contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().is4xxClientError());
}
@Test
public void whenCreateSuccess() throws Exception {
Date date =new Date();
String content = "{\"username\":\"tom\",\"password\":\" \",\"birthday\":"+date.getTime()+"}";
String str=mockMvc.perform(MockMvcRequestBuilders.post("/user/create").contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content)).andExpect(MockMvcResultMatchers.status().isOk())
//校验返回值
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"))
.andReturn().getResponse().getContentAsString();
System.out.println(str);
}
}
image.png
image.png
网友评论