【问题标题】:Converting com.vividsolutions.jts.geom.Point to Dto将 com.vividsolutions.jts.geom.Point 转换为 Dto
【发布时间】:2020-11-17 16:33:11
【问题描述】:

大家好。 我想将 Point 作为字段的类陷入问题,我想将其转换为 DTO 字段。 我的实体和 DTO:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class RentPointDto {
    private String id;

    private String pointName;

    private String type;

    private String coordinate;

}
public class RentPoint {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "Rent point name should'not be null")
    @Column(name = "point_name")
    private String pointName;

    @Enumerated(EnumType.STRING)
    private PointType type;

    @Column(name = "coordinate")
    @NotNull(message = "Coordinate of renting point must be specified!")
    private Point coordinate;

(为简洁起见省略了一堆东西)

我的映射器:

public interface GenericMapper<S, DTO> {

    DTO toDto(S s);

    S fromDto(DTO dto);

    List <S> listFromDto(List <DTO> dto);

    List <DTO> listToDto(List <S> entities);
}

@Mapper(componentModel = "spring", uses = GeometryConverter.class)
public interface RentPointMapper extends GenericMapper <RentPoint, RentPointDto> {

}

和转换器类:

@Component
public class GeometryConverter {
    public Point unMap(String str) throws ParseException {
        return (Point) new WKTReader().read( str );
    }

    public String map(Point point) {
        return point.toText();
    }
}

为 Mapper 生成的 Impl:

@Autowired
    private GeometryConverter geometryConverter;

    @Override
    public RentPointDto toDto(RentPoint s) {
        if ( s == null ) {
            return null;
        }

        RentPointDto rentPointDto = new RentPointDto();

        if ( s.getId() != null ) {
            rentPointDto.setId( String.valueOf( s.getId() ) );
        }
        rentPointDto.setPointName( s.getPointName() );
        if ( s.getType() != null ) {
            rentPointDto.setType( s.getType().name() );
        }
        rentPointDto.setCoordinate( geometryConverter.map( s.getCoordinate() ) );

        return rentPointDto;
    }

    @Override
    public RentPoint fromDto(RentPointDto dto) {
        if ( dto == null ) {
            return null;
        }

        RentPoint rentPoint = new RentPoint();

        if ( dto.getId() != null ) {
            rentPoint.setId( Long.parseLong( dto.getId() ) );
        }
        rentPoint.setPointName( dto.getPointName() );
        if ( dto.getType() != null ) {
            rentPoint.setType( Enum.valueOf( PointType.class, dto.getType() ) );
        }
        try {
            rentPoint.setCoordinate( geometryConverter.unMap( dto.getCoordinate() ) );
        }
        catch ( ParseException e ) {
            throw new RuntimeException( e );
        }

        return rentPoint;
    }

毕竟,我得到了:

java:无法将属性“PointrentPoint.coordinate”映射到“String rentPoint.coordinate”。考虑声明/实现一个映射方法: “字符串映射(点值)”。

【问题讨论】:

    标签: mapstruct


    【解决方案1】:

    Mybe晚了,我最近开发了一个名为beanknife的注解处理器,它支持从任何类生成DTO。您需要通过注释进行配置。但是你不需要改变原来的类。该库支持在单独的类上进行配置。当然,您可以选择您想要的和不需要的属性。您可以通过配置类中的静态方法添加新属性。对于您的问题:

    @ViewOf(value = RentPoint.class, genName="RentPointDto" includes = {"id", "pointName"})
    class ConfigureObj {
        @NewViewProperty("type")
        public static String type(RentPoint source) {
            // convert the source to String
        }
        @NewViewProperty("coordinate")
        public static String coordinate(RentPoint source) {
            // convert the coordinate to Point
        }
    }
    

    会生成

    // meta class, you can use it to reference the property name in a safe way.
    class RentPointMeta {
        public final String id = "id";
        public final String pointName = "pointName";
        public final String type = "type";
        public final String coordinate = "coordinate";
    }
    
    // generated DTO class. The actual one will be more complicate, there are many other method.
    public class RentPointDto {
        private String id;
        private String pointName;
        private String type;
        private String coordinate;
        // some getters and setters. no setters by default.
        public static RentPoint read(RentPointDto source) { ... }
        public static List<RentPoint> read(List<RentPointDto> source) { ... }
        // other read methods.
    }
    

    没有反射,没有魔法。所有逻辑都显示在流派类源中。 这个库主要是用来生成只读对象的,所以只有read方法,没有write方法。 (不过支持dto的setter方法。)也许下个版本会增加写支持

    【讨论】:

      【解决方案2】:

      删除GenericMapper接口后我的问题解决了

      public interface GenericMapper<S, DTO> {
      
          DTO toDto(S s);
      
          S fromDto(DTO dto);
      
          List <S> listFromDto(List <DTO> dto);
      
          List <DTO> listToDto(List <S> entities);
      }
      

      由于 MapStruct 不支持映射器中的参数,我无法添加注释 @Mapper(uses=..)。这导致 RentPointMapper 中缺少几何转换器。

      【讨论】:

      • 我不理解为什么它不应该工作的原因。在我看来,这应该有效。您能否提出一个问题,以便我们进行调查?
      猜你喜欢
      • 1970-01-01
      • 2016-06-08
      • 2019-04-22
      • 2014-05-16
      • 1970-01-01
      • 2016-06-10
      • 2012-08-06
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多