在我正在研究的示例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...
}
Run Code Online (Sandbox Code Playgroud)
DTO:
public class PositionDto {
private Integer id;
private String name;
// getters/setters...
}
Run Code Online (Sandbox Code Playgroud)
休息控制器:
@RestController
public class PositionController {
@Autowired
private PositionService positionService;
@RequestMapping("/position")
public PositionDto get() {
final PositionDto positionDto = positionService.getPosition(1);
return positionDto;
}
}
Run Code Online (Sandbox Code Playgroud)
服务类:
@Service
public class PositionServiceImpl implements PositionService {
@Autowired
private PositionRepository positionRepository;
@Autowired
private OrikaBeanMapper mapper;
@Transactional(readOnly = true)
@Override …Run Code Online (Sandbox Code Playgroud)