美文网首页
常见的注解

常见的注解

作者: 金石_832e | 来源:发表于2019-05-28 11:37 被阅读0次

常见注解

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

相关文章

  • 常见的注解

    常见注解 lombok @Data -- getter和setter方法@Getter -- getter方法@S...

  • 2018-04-12

    注解机制及其原理 什么是注解 注解也叫元数据,例如我们常见的@Override和@Deprecated,注解...

  • 常见注解

    常见注解 lombok @Data -- getter和setter方法@Getter -- getter方法@S...

  • Java Annotations

    Java常见注解 JDK自带注解 @Override:重写@Deprecated:过时@Suppvisewarni...

  • springboot注解

    为什么用注解? 用注解可以减少配置文件使用,使用面向对象 常见注解 @SpringBootApplication ...

  • java并发编程: 基础部分

    捡重要的记录 目的 常见问题: 用synchronized修正上面的代码: 常见文档性注解说明:下面都是文档性注解...

  • 5.注解

    1.注解的概念 2.常见的注解 3.定义注解语法 4.带有属性的注解 5.注解注意的细节: 5.元注解的概念(在注...

  • (Android)注解系列-注解基本概念

    写在前面 本文目的:让你能知道注解是什么东西,看懂注解的元注解,以及常见的注解。其他:本文的注解有动词意思也有名词...

  • Java注解

    一、什么是注解 注解也叫元数据, 例如我们常见的@Override和@Deprecated等。注解是JDK1.5引...

  • Spring框架中最常见的注解浅谈

    Spring常用注解浅谈 我先列举一些Spring中的常见注解:@Autowired @Resource @Qua...

网友评论

      本文标题:常见的注解

      本文链接:https://www.haomeiwen.com/subject/dtvrtctx.html