null指针异常

vfwfrxfs  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(277)

我正在使用springboot创建一个web服务示例。当我在localhost中运行它并用postman进行测试时,它运行得很好。但是当我在aws lambda上部署应用程序时,它抛出一个nullpointerexception。
当我在本地和aws lambda上进行测试时,它运行良好,直到我开始使用@autowired
这是项目结构
endpont或handle请求位于logincontroller类中

  1. @Controller
  2. @RequestMapping(value="api/auth/v1")
  3. public class LoginController {
  4. @Autowired
  5. IClientCredentialService _clientCredentialService;
  6. @RequestMapping(value = "/login", method = RequestMethod.POST, headers = "Accept=application/json")
  7. public ResponseEntity<?> login(@RequestBody ClientCredential clientCredential){
  8. //For testing from Postman and AWS, clientCredential is not null.
  9. if(clientCredential == null) {
  10. return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
  11. }
  12. if(clientCredential.getClientId() == null || clientCredential.getClientId().equals("")) {
  13. return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
  14. }
  15. if(clientCredential.getClientSecret() == null || clientCredential.getClientSecret().equals("")) {
  16. return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
  17. }
  18. else {
  19. ClientCredential client = _clientCredentialService.findClientCredential(clientCredential.getClientId(), clientCredential.getClientSecret());
  20. //TODO Obtener token aquí
  21. Token token = new Token();
  22. token.setCode(HttpStatus.OK.value());
  23. token.setDescription(HttpStatus.OK.name());
  24. //Temp
  25. token.setAccessToken("123");
  26. token.setCreatedDate("Hoy");
  27. token.setUpdatedDate("Hoy");
  28. token.setExpiresIn("Mañana");
  29. token.setStartIn("Hoy");
  30. token.setTokenId("321");
  31. token.setTokenType("bearer");
  32. token.setUpdatedDate("hoy");
  33. return new ResponseEntity<Token>(token, HttpStatus.OK);
  34. }

接口iclientcredentialservice有一个方法,类clientcredentialserviceimpl实现该接口:

  1. @Service("clientCredentialService")
  2. @Transactional
  3. public class ClientCredentialServiceImpl implements IClientCredentialService{
  4. @Override
  5. public ClientCredential findClientCredential(String clientId, String clientSecret) {
  6. return new ClientCredential();
  7. }
  8. }

这是主要课程:

  1. @SpringBootApplication
  2. public class AwsLambdaSurveyAuthServerLoginV1Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(AwsLambdaSurveyAuthServerLoginV1Application.class, args);
  5. }
  6. }

类clientcredential和token有一个构造函数和一些getter和setter。
当我从postman运行它时,它工作正常,我得到一个200 http响应:

  1. {
  2. "code": 200,
  3. "description": "OK",
  4. "tokenId": "321",
  5. "accessToken": "123",
  6. "tokenType": "bearer",
  7. "expiresIn": "Mañana",
  8. "startIn": "Hoy",
  9. "createdDate": "Hoy",
  10. "updatedDate": "hoy"
  11. }

但是,当我从aws lambda运行它时,我得到一个错误:

  1. {
  2. "errorMessage": "java.lang.NullPointerException",
  3. "errorType": "java.lang.NullPointerException",
  4. "stackTrace": [
  5. "com.gns.survey.authserver.login.controller.LoginController.login(LoginController.java:47)",
  6. "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
  7. "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
  8. "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
  9. "java.lang.reflect.Method.invoke(Method.java:498)"
  10. ]
  11. }

有人知道错误是什么吗?我真的很感谢你的帮助。
/编辑:具有空验证的方法

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题