Spring REST API用のエンティティからDTOへの変換

Spring REST APIのエンティティからDTOへの変換

1. 概要

このチュートリアルでは、クライアントに公開されるbetween the internal entities of a Spring application and the external DTOs(データ転送オブジェクト)で発生する必要のある変換を処理します。

参考文献:

Dozerによるマッピングのガイド

Dozerは、あるオブジェクトから別のオブジェクトにデータを属性ごとにコピーし、属性名間のマッピングをサポートし、型変換を行い、その他多くのことを行うJava BeanからJava Beanへのマッパーです。

MapStructのクイックガイド

MapStructを使用するための迅速かつ実用的なガイド

2. モデルマッパー

このエンティティを実行するために使用するメインライブラリ-DTO変換-ModelMapperを紹介することから始めましょう。

pom.xmlでこの依存関係が必要になります。


    org.modelmapper
    modelmapper
    2.3.2

このライブラリの新しいバージョンがあるかどうかを確認するには、go hereを使用します。

次に、Spring構成でModelMapperBeanを定義します。

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

3. DTO

次に、この両面問題のDTO側–PostDTOを紹介しましょう。

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
}

2つのカスタム日付関連メソッドは、クライアントとサーバーの間で日付の変換を処理することに注意してください。

  • getSubmissionDateConverted()メソッドは、日付文字列をサーバーのタイムゾーンの日付に変換して、Postエンティティの永続化に使用します

  • setSubmissionDate()メソッドは、DTOの日付を現在のユーザーのタイムゾーンの投稿の日付に設定することです。

4. サービス層

次に、サービスレベルの操作を見てみましょう。これは明らかにエンティティ(DTOではなく)で機能します。

public List getPostsList(
  int page, int size, String sortDir, String sort) {

    PageRequest pageReq
     = PageRequest.of(page, size, Sort.Direction.fromString(sortDir), sort);

    Page posts = postRepository
      .findByUser(userService.getCurrentUser(), pageReq);
    return posts.getContent();
}

次に、サービスの上のレイヤーであるコントローラーレイヤーについて見ていきます。 これは、実際に変換が行われる場所です。

5. コントローラー層

次に、Postリソースの単純なREST APIを公開して、標準のコントローラー実装を見てみましょう。

ここでは、いくつかの簡単なCRUD操作を示します。作成、更新、取得、すべて取得です。 そして、操作が非常に簡単であることを考えると、we are especially interested in the Entity-DTO conversion aspects

@Controller
class PostRestController {

    @Autowired
    private IPostService postService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ModelMapper modelMapper;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List getPosts(...) {
        //...
        List 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);
    }
}

そしてここにour conversion from Post entity to PostDto:があります

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

そして、これが変換from DTO to an entityです:

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;
}

ご覧のとおり、モデルマッパーの助けを借りてthe conversion logic is quick and simple –マッパーのmap APIを使用し、変換ロジックを1行も記述せずにデータを変換しています。

6. 単体テスト

最後に、エンティティと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());
    }
}

7. 結論

これは、これらの変換を手動で記述する代わりに、モデルマッパーライブラリを使用したsimplifying the conversion from Entity to DTO and from DTO to Entity in a Spring REST APIに関する記事でした。

例の完全なソースコードは、GitHub projectにあります。