Autowiring inside abstract class made for mapstruct

I am trying to build a REST Controller using Spring. To format the data for readability and more integration, I have used Mapstruct. Here's how I wrote Mapper.

public abstract class DeviceDataMapper {

DeviceService deviceService;

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

    @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();
    try {
    } catch (Exception e) {

    return deviceData;

The API Controller function looks like this

@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) {
    return deviceData;

The output deviceData returns fine except for one detail. I couldn't get to this function deviceService.findDeviceTemplateById(toDevice.getDeviceTemplateId() (where deviceService is autowired). The error stack trace shows me NullPointerException. So I am wondering whether is there any general rule about the accessibility of the autowired resources in abstract class? Or is the way I am instantiating that makes this function inaccessible? What should I change to make it work? I have also tried with @Inject from javax.inject with same result.


In order for @Autowired to work, the DeviceDataMapper 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 deviceService into it from your controller.

Need Your Help

Onclick php update query

php onclick updates

Please help.. I'm abit on trouble with this code. I'm confuse! I use the <form> <input type="submit">. The ID is identified however if i click the one to be submitted the other ID appea...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.