为mapstruct

时间:2015-05-08 09:50:28

标签: java spring rest abstract-class mapstruct

我正在尝试使用Spring构建REST控制器。为了格式化数据以提高可读性和集成度,我使用了Mapstruct。这是我写Mapper的方式。

@Mapper
public abstract class DeviceDataMapper {

@Autowired
DeviceService deviceService;

public static DeviceDataMapper INSTANCE = Mappers.getMapper(DeviceDataMapper.class);

@Mappings({
    @Mapping(source = "deviceId", target = "iddevice"),
    @Mapping(source = "deviceName", target = "name")
})
public abstract TODevice deviceToTODevice(DeviceData device);

public DeviceData toDeviceToDeviceData(TODevice toDevice){
    DeviceData deviceData = new DeviceData();
    deviceData.setDeviceId(toDevice.getIddevice());
    deviceData.setDeviceName(toDevice.getName());
    deviceData.setDeviceTemplateId(toDevice.getDeviceTemplateId());
    try {
deviceData.setDeviceTemplateName(deviceService.findDeviceTemplateById(toDevice.getDeviceTemplateId()).getName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return deviceData;
}}

API控制器功能如下所示

@RequestMapping(value = "/{deviceId}",method = RequestMethod.GET)
public @ResponseBody DeviceData get(@PathVariable int deviceId) {
    DeviceData deviceData=new DeviceData();
    try {
        deviceData =    DeviceDataMapper.INSTANCE.toDeviceToDevice(deviceService.findOne(deviceId));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return deviceData;
}

除一个细节外,输出deviceData返回正常。我无法使用此函数deviceService.findDeviceTemplateById(toDevice.getDeviceTemplateId()(其中deviceService是自动装配的)。错误堆栈跟踪显示NullPointerException。所以我想知道抽象类中自动装配资源的可访问性是否有任何一般规则?或者我实例化的方式使得这个功能无法访问?我应该改变什么来使它工作?我也尝试使用来自@Inject的{​​{1}},结果相同。

2 个答案:

答案 0 :(得分:9)

您可以使用Spring作为映射器的组件模型:

@Mapper(componentModel="spring")
public abstract class DeviceDataMapper {
    ...
}

通过这种方式,您可以将依赖项注入其中(例如,它使用的其他手工编写)以及将映射器注入其他类而不是诉诸INSTANCE模式。

答案 1 :(得分:2)

In order for $ **mvn dependency:tree -Dverbose -Dincludes=aopalliance** [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building petshop cli 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ cli --- [INFO] com.sample.petshop:cli:jar:1.0 [INFO] \- **org.springframework:spring-context**:jar:4.1.3.RELEASE:compile [INFO] \- org.springframework:spring-aop:jar:4.1.3.RELEASE:compile [INFO] \- **aopalliance:aopalliance**:jar:1.0:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.865s [INFO] Finished at: Fri May 08 15:12:01 IST 2015 [INFO] Final Memory: 14M/223M [INFO] ------------------------------------------------------------------------ to work, the The servlets named [FileUploadDBServlet] and [com.db.FileUploadDBServlet] are both mapped to the url-pattern [/FileUploadDBServlet] which is not permitted class needs to be a Spring bean. It will not work if you instantiate it yourself.

Either make it a Spring bean and use it like one, or pass a reference to @Autowired into it from your controller.