如果实体具有Enum变量,如何从实体到DTO? [英] How from Entity to DTO if Entity has an Enum variable?

查看:340
本文介绍了如果实体具有Enum变量,如何从实体到DTO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我所有实体的DTO版本。我对具有一个Enum值的实体有疑问。这是我的实体:

  @Getter 
@Setter
@Table(name = TIPOS_MOVIMIENTO)
@Entity

公共类TipoMovimiento {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
@Convert(converter = TipoMovEnumConverter.class)
私人TipoMov tipo;

public String getTipo(){
return tipo.getTipoNombre();
}

@OneToMany(mappedBy = tipoMov)
私人列表< Movimiento> movimientos;

不,我没有@Enumerated,因为我遵循了一个教程: JPA 2.1属性转换器–更好的方法坚持枚举我不得不将其删除。如您所见,我使用转换器。


这是我上一个实体的DTO:

  @Getter 
公共类TipoMovimientoDto实现DtoEntity {


@Convert(converter = TipoMovEnumConverter.class)//我什至不知道是否在这里写下!
私人TipoMov Tipo;
}

我关注


如果您知道这样做的更好方法,请告诉我,但这不是我的问题现在。


让我向您展示枚举:

 公共枚举TipoMov {

INGRESO( Ingreso),
PRESTAMO( Prestamo),
PAGO( Pago),
AMORTIZACION(Amortización),
INTERES(Interés);

私有字符串tipoNombre;


公共字符串getTipoNombre(){
return tipoNombre;
}


TipoMov(字符串tipoNombre){
this.tipoNombre = tipoNombre;
}


公共静态TipoMov fromDBName(字符串tipoNombre){
开关(ti poNombre){
case Ingreso:
return TipoMov.INGRESO;

案件Préstamo:
返还TipoMov.PRESTAMO;

案子 Pago:
返还TipoMov.PAGO;

案件Amortización:
返还TipoMov.AMORTIZACION;

案例Interés:
返回TipoMov.INTERES;

默认值:
抛出new IllegalArgumentException(不支持ShortName [ + tipoNombre
+]。
}
}




}

问题是,如果将其转换为DTO版本,则无法在Postman中获得输出。我得到没有DTO的适当输出。我正在使用REST服务。让我向您展示服务和控制器。
(它们包括没有DTO和DTO(不起作用)的两个版本)。


ServiceImpl

  @Service 
公共类TipoMovimientoServiceImpl实现TipoMovimientoService {

@Autowired
TipoMovimientoRepository存储库;

@Autowired
DtoUtils dtoUtils;


公共列表< DtoEntity> findAllDto(){

List< TipoMovimiento> tiposMovimiento = repository.findAll();
List< DtoEntity> tiposMovimientoDto = new ArrayList();

((TipoMovimiento tipoMovimiento:tiposMovimiento){

DtoEntity tipoMovimientoDto = dtoUtils.convertToDto(tipoMovimiento,新的TipoMovimientoDto());
tiposMovimientoDto.add(tipoMovimientoDto);
}

return tiposMovimientoDto;

}

公共列表< TipoMovimiento> findAll(){

List< TipoMovimiento> tiposMovimiento = repository.findAll();

返程小费Movimiento;

}


}

服务接口

 公共接口TipoMovimientoService {


List< DtoEntity> findAllDto();
List< TipoMovimiento>找到所有();


}

控制器:

  @RestController 
公共类PruebasController {


@Autowired
TipoMovimientoService服务;

@GetMapping( tiposmovdto)
public ResponseEntity< List< DtoEntity> > findAllDto(){
return ResponseEntity.ok(service.findAllDto());
}

@GetMapping( tiposmov)
public ResponseEntity< List< TipoMovimiento> > findAll(){
return ResponseEntity.ok(service.findAll());
}

}

正如我所说,nonDto版本工作得很好,但是DTO版本号这不是DTO转换器的错,因为我还有其他REST服务(没有枚举)可以与DTO完美配合。这是使Enum和Dto兼容的问题!

解决方案

我明白了!!!我从来没有想过这会起作用。

  @Getter 
公共类TipoMovimientoDto实现DtoEntity {

私有TipoMov tipo ;

}

我刚刚在上面的代码(Dto)中进行了更改:


私人TipoMov tipo;



私人 String tipo;


我无法解释如何使用String而不是Enum将Entity中的Enum转换为DTO ...但这是可行的!


如果您遇到相同的问题...这是我的Enum和String之间的属性转换器。

  @Converter(autoApply = true)
公共类TipoMovEnumConverter实现AttributeConverter< TipoMov ,字符串> {

public String convertToDatabaseColumn(TipoMov tipoMov){
return tipoMov.getTipoNombre();
}

public TipoMov convertToEntityAttribute(String dbData){
return dbData == null吗? null:TipoMov.fromDBName(dbData);
}

}

仍然需要在上面的Entity类中使用它枚举变量的值:

  @Convert(converter = TipoMovEnumConverter.class)

但在DTO中不是必需的。只需在DTO中使用String而不是Enum!


I'm creating DTO versions of all my entities. I have a problem with an entity that has one Enum value. This is my entity:

@Getter
@Setter
@Table(name = "TIPOS_MOVIMIENTO")
@Entity

public class TipoMovimiento {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
@Convert(converter = TipoMovEnumConverter.class)
private TipoMov tipo;

public String getTipo() {
    return tipo.getTipoNombre();
}

@OneToMany(mappedBy = "tipoMov")
private List<Movimiento> movimientos;

No, I don't have @Enumerated because I followed a tutorial: "JPA 2.1 Attribute Converter – The better way to persist enums" and I had to remove it. I use a converter, as you can see.

This is my DTO of the previous entity:

@Getter
public class TipoMovimientoDto implements DtoEntity {


@Convert(converter = TipoMovEnumConverter.class) //I don't even know if write this here!!!!!
private TipoMov tipo;
}

The reason why I've followed that tutorial ↑ is because I wanted to write in database the variable values (tipoNombre) of enum (not enum name itself) because format. I want to store it in DB with accents, and I want to show it in Postman or whatever REST Client app with accents! Don't tell me anything about format it in front-end because this project is only back-end :( Well, I think you will understand what I found with this with a image:

If you know a better way to do this, let me know, but this is not my problem now.

Let me show you the Enum:

public enum TipoMov {

INGRESO("Ingreso"),
PRESTAMO("Prestamo"),
PAGO("Pago"),
AMORTIZACION("Amortización"),
INTERES("Interés");

private String tipoNombre;


public String getTipoNombre() {
    return tipoNombre;
}


TipoMov(String tipoNombre) {
    this.tipoNombre = tipoNombre;
}


public static TipoMov fromDBName(String tipoNombre) {
    switch (tipoNombre) {
    case "Ingreso":
        return TipoMov.INGRESO;

    case "Préstamo":
         return TipoMov.PRESTAMO;

    case "Pago":
         return TipoMov.PAGO;

    case "Amortización":
         return TipoMov.AMORTIZACION;
         
    case "Interés":
     return TipoMov.INTERES;

    default:
        throw new IllegalArgumentException("ShortName [" + tipoNombre
                + "] not supported.");
    }
}




}

The problem is that I can't get the output in Postman if I convert this to DTO version. I get the appropiate output without DTO. I'm using REST services. Let me show you the services and controller. (They include both versions, without DTO and with DTO (that is not working)).

ServiceImpl

@Service
public class TipoMovimientoServiceImpl implements TipoMovimientoService {

@Autowired
TipoMovimientoRepository repository;

@Autowired
DtoUtils dtoUtils;


public List<DtoEntity> findAllDto() {

    List<TipoMovimiento> tiposMovimiento = repository.findAll();
    List<DtoEntity> tiposMovimientoDto = new ArrayList();
    
    for (TipoMovimiento tipoMovimiento : tiposMovimiento) {
    
        DtoEntity tipoMovimientoDto= dtoUtils.convertToDto(tipoMovimiento, new TipoMovimientoDto());
        tiposMovimientoDto.add(tipoMovimientoDto);
    }

    return tiposMovimientoDto;
    
}

public List<TipoMovimiento> findAll() {

    List<TipoMovimiento> tiposMovimiento = repository.findAll();

    return tiposMovimiento;
    
}


}

Service Interface

public interface TipoMovimientoService {


List<DtoEntity> findAllDto();
List<TipoMovimiento> findAll();


}

Controller:

@RestController
public class PruebasController {


@Autowired
TipoMovimientoService service;

@GetMapping("tiposmovdto")  
public ResponseEntity <List <DtoEntity> > findAllDto() {
    return ResponseEntity.ok(service.findAllDto());
}

@GetMapping("tiposmov") 
public ResponseEntity <List <TipoMovimiento> > findAll() {
    return ResponseEntity.ok(service.findAll());
}

}

As I said, the nonDto version works perfectly, but DTO version no. Is not the fault of DTO converter, because I have other REST services (that don't have enums) working perfectly with DTO. This is a problem about making compatible Enum and Dto!

解决方案

I got it!!! I never thought this would work.

@Getter
public class TipoMovimientoDto implements DtoEntity {

private TipoMov tipo;

}

I just changed in the code above (Dto):

private TipoMov tipo;

to

private String tipo;

I can't explain how Enum from Entity could have been converted to DTO, using String instead Enum... But that worked!

In case you have the same problem... this is my Attribute Converter between Enum and String

@Converter(autoApply = true)
public class TipoMovEnumConverter implements AttributeConverter<TipoMov, String> { 

public String convertToDatabaseColumn(TipoMov tipoMov) {
    return tipoMov.getTipoNombre();
}

    public TipoMov convertToEntityAttribute(String dbData) {
        return  dbData == null ? null : TipoMov.fromDBName(dbData);
    }
    
}

Is still necessary to use it in Entity class, above of the enum variable:

@Convert(converter = TipoMovEnumConverter.class)

But not necessary in DTO. Just use String instead Enum in DTO!

这篇关于如果实体具有Enum变量,如何从实体到DTO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆