Spring Boot webapp中的Orika ClassCastException

时间:2015-10-25 00:47:36

标签: java spring spring-mvc spring-boot orika

我正在使用示例Spring Boot webapp将实体映射到DTO并使用Orika进行奇怪的ClassCastException。当我尝试在嵌入式Tomcat中部署的应用程序上进行映射时,我得到了异常,但我可以在JUnit测试上下文中完成映射。这是相关的课程(它们都非常简单):

JPA实体:

@Entity
public class Position {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    // getters/setters...
}

DTO:

public class PositionDto {

    private Integer id;
    private String name;
    // getters/setters...
}

休息控制器:

@RestController
public class PositionController {

    @Autowired
    private PositionService positionService;

    @RequestMapping("/position")
    public PositionDto get() {
        final PositionDto positionDto = positionService.getPosition(1);
        return positionDto;
    }
}

服务类:

@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionRepository positionRepository;
    @Autowired
    private OrikaBeanMapper mapper;

    @Transactional(readOnly = true)
    @Override
    public PositionDto getPosition(final Position.ID id) {
        // This returns a populated Position object with id=1 and name = "Creator"
        final Position position = positionRepository.findOne(id.getId());
        // This is where the mapping occurs
        return mapper.map(position, PositionDto.class);
    }
}

OrikaBeanMapper类:

@Component
public class OrikaBeanMapper extends ConfigurableMapper implements ApplicationContextAware {        

    public OrikaBeanMapper() {
        super(false);
    }

    @Override
    protected void configureFactoryBuilder(final   DefaultMapperFactory.Builder factoryBuilder) {
        factoryBuilder.mapNulls(false);
    }
    // Omitted non-important methods

}

这是ClassCastException的堆栈跟踪:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is ma.glasnost.orika.MappingException: While attempting the following mapping:
sourceClass = class com.dlizarra.startuphub.position.Position
destinationType = com.dlizarra.startuphub.position.PositionDto
resolvedStrategy = InstantiateAndUseCustomMapperStrategy<Position, PositionDto> {customMapper: GeneratedMapper<Position, PositionDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [], usedTypes: [] }, unenhancer: ma.glasnost.orika.unenhance.BaseUnenhancer@73c3e10e, objectFactory: DefaultConstructorObjectFactory<PositionDto>}
Error occurred: java.lang.ClassCastException: com.dlizarra.startuphub.position.Position cannot be cast to com.dlizarra.startuphub.position.Position
-----begin dump of current state-----------------------------
Registered object factories: 1 (approximate size: 110.8 kB)
    [PositionDto] : {Position=DefaultConstructorObjectFactory<PositionDto>}
-------------------------------------------------------------------------------
Registered mappers: 1 (approximate size: 17,643.0 kB)
    [0] : GeneratedMapper<Position, PositionDto> {usedConverters: [], usedMappers: [], usedMapperFacades: [], usedTypes: [] }
-------------------------------------------------------------------------------
Registered concrete types: 5 (approximate size: 294.3 kB)
  [interface java.util.List] : ArrayList<Object>
  [interface java.util.Set] : LinkedHashSet<Object>
  [interface java.util.Collection] : ArrayList<Object>
  [interface java.util.Map] : LinkedHashMap<Object, Object>
  [interface java.util.Map$Entry] : MapEntry<Object, Object>
  -------------------------------------------------------------------------------

Resolved strategies: 1 (approximate size: 19,850.8 kB)

{source: Position, dest: PositionDto, in-place:false}:  InstantiateAndUseCustomMapperStrategy<Position, PositionDto>
{customMapper: GeneratedMapper<Position, PositionDto> {usedConverters: [],    usedMappers: [], usedMapperFacades: [], usedTypes: [] }, unenhancer:
ma.glasnost.orika.unenhance.BaseUnenhancer@73c3e10e, objectFactory:
DefaultConstructorObjectFactory<PositionDto>}
-------------------------------------------------------------------------------
Unenhance strategy: ma.glasnost.orika.unenhance.BaseUnenhancer@73c3e10e
-----end dump of current state-------------------------------] with root cause
java.lang.ClassCastException: com.dlizarra.startuphub.position.Position cannot be cast to com.dlizarra.startuphub.position.Position
at ma.glasnost.orika.generated.Orika_PositionDto_Position_Mapper43322711137530$0.mapAtoB(Orika_PositionDto_Position_Mapper43322711137530$0.java) ~[orika-core-1.4.6.jar:na]
at ma.glasnost.orika.impl.mapping.strategy.UseCustomMapperStrategy.map(UseCustomMapperStrategy.java:67) ~[orika-core-1.4.6.jar:na]
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:742) ~[orika-core-1.4.6.jar:na]

我真的不知道这里发生了什么。我没有得到它试图将位置投射到位置的地方。每个实体/ dto类都会发生这种情况,而不仅仅是位置。

当我对任何方法进行单元测试时,我可以映射任何这些类没有问题,它可以正常工作并且所有字段都被正确映射,所以我不认为它是Orika配置问题。仅当我在嵌入式Tomcat中部署了webapp并且在其余控制器方法中调用映射方法时,才会发生异常。

它是一个简单的Spring Boot应用程序,这是我在其中编写的第一个休息端点。也许我在配置中遗漏了一些东西(我有@EnableAutoConfiguration所以没有那么多配置),但我无法猜测是什么让Orika抛出这个异常。

非常感谢任何有关此处可能发生的事情的想法或提示。

谢谢!

1 个答案:

答案 0 :(得分:5)

我刚刚意识到这个错误的解决方法因为几个月前已经使用Spring Boot 1.4.0(我相信它是这个版本),当他们介绍了通过一个自定义Dev Tools的可能性属性文件。

要解决这个问题,我们只需:

  1. META-INF
  2. 中创建src/main/resources文件夹
  3. 在其中创建spring-devtools.properties文件。
  4. restart.include.orika=/orika-core.*\.jar添加到文件中。
  5. 正如Docs中所述,restart.include将进入“重启”状态。 classloader任何匹配正则表达式的jar。所以我们要包含orika-core-1.4.6.jar文件。