Spring REST API 从实体到 DTO 的转换

介绍

在本文中,咱们将处理 Spring 应用的内部实体与客户端外的 DTO(数据传输对象)之间须要进行的转换。java

模型映射

让咱们从介绍用于执行的实体到 DTO 转换的主库开始 —— ModelMapper。 咱们须要将在 pom.xml 中添加以下依赖:服务器

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.3.2</version>
</dependency>
复制代码

而后,咱们在 Spring 配置中定义 ModelMapper bean:app

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}
复制代码

DTO

接下来,让咱们来介绍这个双面问题的 DTO 方面 —— Post DTO:dom

public class PostDto {
    private static final SimpleDateFormat dateFormat
      = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 
    private Long id;
 
    private String title;
 
    private String url;
 
    private String date;
 
    private UserDto user;
 
    public Date getSubmissionDateConverted(String timezone) throws ParseException {
        dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
        return dateFormat.parse(this.date);
    }
 
    public void setSubmissionDate(Date date, String timezone) {
        dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
        this.date = dateFormat.format(date);
    }
 
    // getters and setters
}
复制代码

注意,两个自定义日期相关方法,处理客户端和服务器之间的日期来回转换:post

  • getSubmissionDateConverted() 方法将Date字符串转换为服务器时区中的日期,以在持久化 Post 实体中使用它
  • setSubmissionDate() 方法用于将 DTO 的日期设置为当前用户时区中的 Post 的 Date

Service

如今让咱们看看一个 Service 层的操做 —— 显然它将与实体(而不是DTO)一块儿工做:单元测试

public List<Post> getPostsList( int page, int size, String sortDir, String sort) {
  
    PageRequest pageReq
     = PageRequest.of(page, size, Sort.Direction.fromString(sortDir), sort);
  
    Page<Post> posts = postRepository
      .findByUser(userService.getCurrentUser(), pageReq);
    return posts.getContent();
}
复制代码

咱们接下来将看到服务的上层 —— Controller 层,这也是转换实际发生的地方。学习

Controller

如今让咱们看看一个标准的 Controller 实现,为 Post 公开简单的 REST API。
咱们将在这里展现几个简单的 CRUD 操做:建立、更新、获取一个和获取所有。测试

@Controller
class PostRestController {
 
    @Autowired
    private IPostService postService;
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private ModelMapper modelMapper;
 
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List<PostDto> getPosts(...) {
        //...
        List<Post> posts = postService.getPostsList(page, size, sortDir, sort);
        return posts.stream()
          .map(post -> convertToDto(post))
          .collect(Collectors.toList());
    }
 
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public PostDto createPost(@RequestBody PostDto postDto) {
        Post post = convertToEntity(postDto);
        Post postCreated = postService.createPost(post));
        return convertToDto(postCreated);
    }
 
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public PostDto getPost(@PathVariable("id") Long id) {
        return convertToDto(postService.getPostById(id));
    }
 
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public void updatePost(@RequestBody PostDto postDto) {
        Post post = convertToEntity(postDto);
        postService.updatePost(post);
    }
}
复制代码

这是咱们从 Post 实体到 PostDto 的转换:this

private PostDto convertToDto(Post post) {
    PostDto postDto = modelMapper.map(post, PostDto.class);
    postDto.setSubmissionDate(post.getSubmissionDate(), 
        userService.getCurrentUser().getPreference().getTimezone());
    return postDto;
}
复制代码

这是从DTO到实体的转换:url

private Post convertToEntity(PostDto postDto) throws ParseException {
    Post post = modelMapper.map(postDto, Post.class);
    post.setSubmissionDate(postDto.getSubmissionDateConverted(
      userService.getCurrentUser().getPreference().getTimezone()));
  
    if (postDto.getId() != null) {
        Post oldPost = postService.getPostById(postDto.getId());
        post.setRedditID(oldPost.getRedditID());
        post.setSent(oldPost.isSent());
    }
    return post;
}
复制代码

所以,正如您所看到的,在模型映射的帮助下, 转换逻辑快速而简单 —— 咱们使用映射的 map API,无需编写任何转换逻辑就能够得到转换后的数据。

单元测试

最后,让咱们作一个很是简单的测试,以确保实体和 DTO 之间的转换工做正常:

public class PostDtoUnitTest {
 
    private ModelMapper modelMapper = new ModelMapper();
 
    @Test
    public void whenConvertPostEntityToPostDto_thenCorrect() {
        Post post = new Post();
        post.setId(Long.valueOf(1));
        post.setTitle(randomAlphabetic(6));
        post.setUrl("www.test.com");
 
        PostDto postDto = modelMapper.map(post, PostDto.class);
        assertEquals(post.getId(), postDto.getId());
        assertEquals(post.getTitle(), postDto.getTitle());
        assertEquals(post.getUrl(), postDto.getUrl());
    }
 
    @Test
    public void whenConvertPostDtoToPostEntity_thenCorrect() {
        PostDto postDto = new PostDto();
        postDto.setId(Long.valueOf(1));
        postDto.setTitle(randomAlphabetic(6));
        postDto.setUrl("www.test.com");
 
        Post post = modelMapper.map(postDto, Post.class);
        assertEquals(postDto.getId(), post.getId());
        assertEquals(postDto.getTitle(), post.getTitle());
        assertEquals(postDto.getUrl(), post.getUrl());
    }
}
复制代码

总结

这是一篇关于在 Spring REST API 中简化从实体到 DTO 和从 DTO 到实体的转换的文章,方法是使用模型映射库,而不是手工编写这些转换。

欢迎关注个人公众号:曲翎风,得到独家整理的学习资源和平常干货推送。
若是您对个人专题内容感兴趣,也能够关注个人博客:sagowiec.com

相关文章
相关标签/搜索