SpringRESTAPI从实体到DTO的转换

编程

介绍

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

模型映射

让我们从介绍用于执行的实体到 DTO 转换的主库开始 —— ModelMapper。

我们需要将在 pom.xml 中添加如下依赖:

<dependency>

<groupId>org.modelmapper</groupId>

<artifactId>modelmapper</artifactId>

<version>2.3.2</version>

</dependency>

然后,我们在 Spring 配置中定义 ModelMapper bean:

@Bean

public ModelMapper modelMapper() {

return new ModelMapper();

}

DTO

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

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

}

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

  • 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 的转换:

private PostDto convertToDto(Post post) {

PostDto postDto = modelMapper.map(post, PostDto.class);

postDto.setSubmissionDate(post.getSubmissionDate(),

userService.getCurrentUser().getPreference().getTimezone());

return postDto;

}

这是从DTO到实体的转换:

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

以上是 SpringRESTAPI从实体到DTO的转换 的全部内容, 来源链接: utcz.com/z/510656.html

回到顶部