在控制器中将@Autowired注释字段的实例获取为NULL。

时间:2018-02-02 14:01:23

标签: java spring-boot

我正在为我的应用程序创建几个RestAPI端点。但是,我在我的一个控制器中遇到了一些奇怪的问题。

以下是我正在使用的代码,

import com.eprocure.webapi.data.dto.OtpDto;
import ma.glasnost.orika.MapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@CrossOrigin
@RestController
@RequestMapping("/api/otp")
public class OtpController {

    @Autowired
    private MapperFactory  mapperFactory;

    @RequestMapping(value = {"/send", "/resend"}, method = RequestMethod.POST)
    private ResponseEntity<OtpDto> sendOtp(@Valid @RequestBody OtpDto otpDto) {
        /*
        Using instance of @MapperFactory but
        getting mapperFactory NULL.
         */
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

执行上面的代码时,我的mapperFactory总是为NULL。

注意:如果我从上面的控制器中删除@Validated标签,它会以某种方式工作得很好。我不知道原因可能是什么。

但是,我想在其中一个方法中使用@Validated注释。

我研究了SO,但截至目前没有任何帮助。

有人能让我知道我遇到的问题是什么吗?

提前致谢。

0 个答案:

没有答案