知識庫 / Spring RSS 訂閱

Spring REST API 實體到DTO轉換

REST,Spring
HongKong
6
03:59 AM · Dec 06 ,2025

1. 概述

在本教程中,我們將處理 Spring 應用內部實體與發佈回客户端的外部 DTO(數據傳輸對象)之間的轉換。

2. 映射器

我們首先介紹我們將使用的主要庫,用於執行實體-DTO之間的轉換,即 ModelMapper

我們需要在 pom.xml 中添加以下依賴:

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>3.2.0</version>
</dependency>

要檢查該庫是否有更新版本,請前往這裏

然後,我們將定義 ModelMapper Bean 在我們的 Spring 配置中:

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}

3. The DTO

Next let’s introduce the DTO side of this two-sided problem, 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);
    }

    // standard getters and setters
}

請注意,這兩個自定義日期相關的處理方法負責處理客户端和服務器之間日期格式的轉換。

  • getSubmissionDateConverted() 方法將日期 String 轉換為服務器時區中的 Date 對象,用於在持久化 Post 實體中進行使用
  • setSubmissionDate() 方法用於將 DTO 的日期設置為當前用户時區中的 PostDate

4. 服務層

現在讓我們來看一個服務層操作,它當然會與實體(而不是 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();
}

我們接下來將查看位於服務之上的層級,即控制器層。這是轉換實際發生的地點。

5. 控制器層

接下來,讓我們研究一個標準的控制器實現,以暴露對 Post 資源的簡單 REST API。

我們將演示一些基本的 CRUD 操作:創建、更新、獲取單個記錄和獲取所有記錄。鑑於這些操作相對簡單,我們特別關注實體-DTO 轉換方面:

@Controller
class PostRestController {

    @Autowired
    private IPostService postService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ModelMapper modelMapper;

    @GetMapping
    @ResponseBody
    public List<PostDto> getPosts(...) {
        //...
        List<Post> posts = postService.getPostsList(page, size, sortDir, sort);
        return posts.stream()
          .map(this::convertToDto)
          .collect(Collectors.toList());
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public PostDto createPost(@RequestBody PostDto postDto) {
        Post post = convertToEntity(postDto);
        Post postCreated = postService.createPost(post));
        return convertToDto(postCreated);
    }

    @GetMapping(value = "/{id}")
    @ResponseBody
    public PostDto getPost(@PathVariable("id") Long id) {
        return convertToDto(postService.getPostById(id));
    }

    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.OK)
    public void updatePost(@PathVariable("id") Long id, @RequestBody PostDto postDto) {
        if(!Objects.equals(id, postDto.getId())){
            throw new IllegalArgumentException("IDs don't match");
        }
        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,在不編寫任何轉換邏輯的情況下完成數據轉換。

6. 單元測試

最後,我們做一個非常簡單的測試,以確保實體與DTO之間的轉換能夠正常工作:

public class PostDtoUnitTest {

    private ModelMapper modelMapper = new ModelMapper();

    @Test
    public void whenConvertPostEntityToPostDto_thenCorrect() {
        Post post = new Post();
        post.setId(1L);
        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(1L);
        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());
    }
}

7. 結論

本文詳細介紹瞭如何通過使用 Model Mapper 庫簡化從 Entity 到 DTO,以及從 DTO 到 Entity 的轉換,從而避免手動編寫這些轉換,在 Spring REST API 中實現。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.