2

我正在使用 spring 构建一个简单的 REST 服务。我将我的实体与 DTO 分开,并使用Immutables使 DTO 成为不可变的。我需要 DTO 和 DAO 之间的映射,所以我选择了MapStruct。Mapper 无法检测到我在 DAO 中定义的设置器。

该问题与此问题完全相同。这个问题没有公认的答案,我已经尝试了该问题中的所有建议,但它们不起作用。我不想尝试这个答案,因为我觉得它违背了我使用 Immutables 的目的。@marc-von-renteln 在这里的评论中很好地总结了这个原因

我尝试了@tobias-schulte 提供的答案。但这导致了一个不同的问题。在答案的 Mapper 类中,尝试从映射方法返回 Immutable*.Builder 会引发错误,指出找不到 Immutable 类型。

我已经详尽搜索了针对 MapStruct 和 Immutables 记录的问题,但我无法找到解决方案。不幸的是,很少有例子或人使用 MapStruct 和 Immutables 的组合。mapstruct -examples存储库也没有使用 Immutables 的示例。

我什至尝试为每个 DtTO 定义单独的 Mapper 接口(如 UserStatusMapper)。我只是让它变得更复杂,错误更多。

我创建了一个示例 spring 项目来演示该问题。 GitHub 回购链接。这个演示应用程序与我正在创建的 REST 服务几乎相同。所有数据库(spring-data-jpa,hibernate)的东西都被删除了,我正在使用模拟数据。如果您签出项目并运行演示应用程序,您可以进行两次 API 调用。

GetUser:请求: http://localhost:8080/user/api/v1/users/1 响应:

{
    "id": 0,
    "username": "TestUser",
    "email": "TestUser@demo.com",
    "userStatus": {
        "id": 1,
        "status": 1,
        "statusName": "Active"
    }

Createuser:问题在这里 http://localhost:8080/user/api/v1/users/create 示例输入:

{
    "username": "TestUser",
    "email": "TestUser@demo.com",
    "userStatus": {
        "id": 1,
        "status": 1,
        "statusName": "Active"
    }
}

回复:

{
    "timestamp": "2019-04-28T09:29:24.933+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Type definition error: [simple type, class com.immutablesmapstruct.demo.dto.model.ImmutableUserDto$Builder]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.immutablesmapstruct.demo.dto.model.ImmutableUserDto$Builder`, problem: Cannot build UserDto, some of required attributes are not set [username, email, userStatus]\n at [Source: (PushbackInputStream); line: 9, column: 1]",
    "path": "/user/api/v1/users/create"
}

以下是与问题相关的重要代码:

道: 1. UserDao

public class User {

    // Primary Key. Something that is annotated with @Id
    private int id;
    private String username;
    private String email;
    private UserStatus userStatus;

    private User(Builder builder) {
        id = builder.id;
        username = builder.username;
        email = builder.email;
        userStatus = builder.userStatus;
    }

    public static Builder builder() {
        return new Builder();
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getEmail() {
        return email;
    }

    public UserStatus getUserStatus() {
        return userStatus;
    }

    public static final class Builder {
        private int id;
        private String username;
        private String email;
        private UserStatus userStatus;

        private Builder() {
        }

        public Builder setId(int id) {
            this.id = id;
            return this;
        }

        public Builder setUsername(String username) {
            this.username = username;
            return this;
        }

        public Builder setEmail(String email) {
            this.email = email;
            return this;
        }

        public Builder setUserStatus(UserStatus userStatus) {
            this.userStatus = userStatus;
            return this;
        }

        public User build() {
            return new User(this);
        }

2. 用户状态道:

package com.immutablesmapstruct.demo.dao.model;

/**
 * Status of user.
 * Example: Active or Inactive
 */
public class UserStatus {
    // Primary Key. Something that is annotated with @Id
    private int id;
    // A value of 1 or 0
    private int status;
    // Active , InActive
    private String statusName;

    private UserStatus(Builder builder) {
        id = builder.id;
        status = builder.status;
        statusName = builder.statusName;
    }

    public static Builder builder() {
        return new Builder();
    }

    public int getId() {
        return id;
    }

    public int getStatus() {
        return status;
    }

    public String getStatusName() {
        return statusName;
    }

    public static final class Builder {
        private int id;
        private int status;
        private String statusName;

        private Builder() {
        }

        public Builder setId(int id) {
            this.id = id;
            return this;
        }

        public Builder setStatus(int status) {
            this.status = status;
            return this;
        }

        public Builder setStatusName(String statusName) {
            this.statusName = statusName;
            return this;
        }

        public UserStatus build() {
            return new UserStatus(this);
        }
    }
}

DTO 1. UserDto:

package com.immutablesmapstruct.demo.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@Value.Immutable
@Value.Style(defaults = @Value.Immutable(copy = false), init = "set*")
@JsonSerialize(as = ImmutableUserDto.class)
@JsonDeserialize(builder = ImmutableUserDto.Builder.class)
public abstract class UserDto {

    @Value.Default
    @JsonProperty
    public int id() {
        return 0;
    }

    @JsonProperty
    public abstract String username();

    @JsonProperty
    public abstract String email();

    @JsonProperty
    public abstract UserStatusDto userStatus();

2. UserStatusDto:

package com.immutablesmapstruct.demo.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@Value.Immutable
@Value.Style(defaults = @Value.Immutable(copy = false), init = "set*")
@JsonSerialize(as = ImmutableUserStatusDto.class)
@JsonDeserialize(builder = ImmutableUserStatusDto.Builder.class)
public abstract class UserStatusDto {

    @JsonProperty
    public abstract int id();

    @JsonProperty
    public abstract int status();

    @JsonProperty
    public abstract String statusName();

}

MapStruct 用户映射器:

package com.immutablesmapstruct.demo.dto.mapper;

import com.immutablesmapstruct.demo.dao.model.User;
import com.immutablesmapstruct.demo.dao.model.UserStatus;
import com.immutablesmapstruct.demo.dto.model.UserDto;
import com.immutablesmapstruct.demo.dto.model.UserStatusDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring")
public interface UserMapper {

    UserMapper USER_MAPPER_INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDto userDaoToDto(User user);

    //Problem here.
    User userDtoToDao(UserDto userDto);

    UserStatusDto userStatusDaoToDto(UserStatus userStatusDao);
    UserStatus userStatusDtoToDao(UserStatusDto userStatusDto);
}

如果我查看 MapStruct for 生成的具体方法,userDtoToDao我可以清楚地看到设置器没有被识别。

package com.immutablesmapstruct.demo.dto.mapper;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2019-04-28T02:29:03-0700",
    comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_191 (Oracle Corporation)"
)
@Component
public class UserMapperImpl implements UserMapper {
...
...
    @Override
    public User userDtoToDao(UserDto userDto) {
        if ( userDto == null ) {
            return null;
        }

        com.immutablesmapstruct.demo.dao.model.User.Builder user = User.builder();

        return user.build();
    }
....
....
}
4

1 回答 1

2

Mapstruct 无法识别您在UserDtoand中的吸气剂UserStatusDto

当您将public abstract String username()这些抽象类中的现有方法(如)更改为经典的 getter 时

@JsonProperty("username")
public abstract String getUsername();

MapperImpl包含所需的调用。请注意,@JsonProperty之后需要属性名称本身(因为更改了方法名称)。

以下是完整的课程UserDtoUserStatusDto进行了上述更改:

package com.immutablesmapstruct.demo.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@Value.Immutable
@Value.Style(defaults = @Value.Immutable(copy = false), init = "set*")
@JsonSerialize(as = ImmutableUserDto.class)
@JsonDeserialize(builder = ImmutableUserDto.Builder.class)
public abstract class UserDto {

    @Value.Default
    @JsonProperty("id")
    public int getId() {
        return 0;
    }

    @JsonProperty("username")
    public abstract String getUsername();

    @JsonProperty("email")
    public abstract String getEmail();

    @JsonProperty("userStatus")
    public abstract UserStatusDto getUserStatus();

}

 

package com.immutablesmapstruct.demo.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

@Value.Immutable
@Value.Style(defaults = @Value.Immutable(copy = false), init = "set*")
@JsonSerialize(as = ImmutableUserStatusDto.class)
@JsonDeserialize(builder = ImmutableUserStatusDto.Builder.class)
public abstract class UserStatusDto {

    @JsonProperty("id")
    public abstract int getId();

    @JsonProperty("status")
    public abstract int getStatus();

    @JsonProperty("statusName")
    public abstract String getStatusName();

}

于 2019-04-28T19:13:42.363 回答