Spring Boot webapp 中的 Orika ClassCastException [英] Orika ClassCastException in Spring Boot webapp

查看:51
本文介绍了Spring Boot webapp 中的 Orika ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的示例 Spring Boot web 应用程序中使用 Orika 将实体映射到 DTO 时,我遇到了一个奇怪的 ClassCastException.当我尝试在嵌入式 Tomcat 中对部署的应用程序进行映射时遇到异常,但我可以在 JUnit 测试上下文中很好地进行映射.这是相关的类(它们都很简单):

I'm having a weird ClassCastException while mapping an entity to a DTO with Orika in a sample Spring Boot webapp I'm working on. I get the exception when I attempt to do the mapping on the deployed app in embedded Tomcat, but I can do the mapping just fine in a JUnit test context. This are the relevant classes (they are all very simple):

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 类:

OrikaBeanMapper class:

@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 的堆栈跟踪:

And this is the stacktrace of the 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 类都会发生这种情况,而不仅仅是位置.

I really have no idea what's going on here. I don't get where it is trying to cast Position to Position. This happens with every entity/dto class, not just with Position.

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

I can map any of those classes with no problem when I am unit testing any method, it works perfectly and all the fields get mapped correctly, so I don't think it's an Orika configuration issue. The exception only occurs when I have the webapp deployed in embedded Tomcat and the mapping method gets called within the rest controller method.

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

It's a simple Spring Boot application and this is the first rest endpoint I wrote in it. Maybe I am missing something in the configuration (I have @EnableAutoConfiguration so there's not that much to configure), but I can't guess what is making Orika throw this exception.

任何关于这里可能发生的事情的想法或提示将不胜感激.

Any ideas or hints about what might be happening here would be very much appreciated.

谢谢!

推荐答案

我刚刚意识到有一个解决这个错误的方法,因为几个月前已经有了 Spring Boot 1.4.0(我相信是这个版本),当他们引入可以通过属性文件自定义开发工具.

I just realized there's a workaround for this error since a few months ago already with Spring Boot 1.4.0 (I believe it's this version), when they introduced the possibility to customize Dev Tools through a properties file.

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

To solve this issue we just have to:

  1. src/main/resources 中创建一个 META-INF 文件夹.
  2. 在其中创建 spring-devtools.properties 文件.
  3. restart.include.orika=/orika-core.*\.jar 添加到文件中.
  1. Create a META-INF folder in src/main/resources.
  2. Create spring-devtools.properties file in it.
  3. Add restart.include.orika=/orika-core.*\.jar to the file.

Docsrestart.include 会将任何与 Regex 匹配的 jar 拉入重新启动"类加载器.例如,我们包含 orika-core-1.4.6.jar 文件.

As stated in the Docs, the restart.include will pull up into the 'restart' classloader any jar matching the Regex. So we are including the orika-core-1.4.6.jar file for example.

这篇关于Spring Boot webapp 中的 Orika ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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