null指针异常

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

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

@Controller
@RequestMapping(value="api/auth/v1")
public class LoginController {

   @Autowired
   IClientCredentialService _clientCredentialService;

   @RequestMapping(value = "/login", method = RequestMethod.POST, headers = "Accept=application/json")
   public ResponseEntity<?> login(@RequestBody ClientCredential clientCredential){

    //For testing from Postman and AWS, clientCredential is not null.

    if(clientCredential == null) {
        return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
    }

    if(clientCredential.getClientId() == null || clientCredential.getClientId().equals("")) {
        return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
    }

    if(clientCredential.getClientSecret() == null || clientCredential.getClientSecret().equals("")) {
        return new ResponseEntity<VOGenericResponse>(new VOGenericResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.name()), HttpStatus.BAD_REQUEST);
    }

    else {

        ClientCredential client = _clientCredentialService.findClientCredential(clientCredential.getClientId(), clientCredential.getClientSecret());

        //TODO Obtener token aquí

        Token token = new Token();
        token.setCode(HttpStatus.OK.value());
        token.setDescription(HttpStatus.OK.name());

        //Temp
        token.setAccessToken("123");
        token.setCreatedDate("Hoy");
        token.setUpdatedDate("Hoy");
        token.setExpiresIn("Mañana");
        token.setStartIn("Hoy");
        token.setTokenId("321");
        token.setTokenType("bearer");
        token.setUpdatedDate("hoy");

        return new ResponseEntity<Token>(token, HttpStatus.OK);
    }

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

@Service("clientCredentialService")
@Transactional
public class ClientCredentialServiceImpl implements IClientCredentialService{

    @Override
    public ClientCredential findClientCredential(String clientId, String clientSecret) {

        return new ClientCredential();
    }
}

这是主要课程:

@SpringBootApplication
public class AwsLambdaSurveyAuthServerLoginV1Application {

    public static void main(String[] args) { 
     SpringApplication.run(AwsLambdaSurveyAuthServerLoginV1Application.class, args);
    }
}

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

{
"code": 200,
"description": "OK",
"tokenId": "321",
"accessToken": "123",
"tokenType": "bearer",
"expiresIn": "Mañana",
"startIn": "Hoy",
"createdDate": "Hoy",
"updatedDate": "hoy"
}

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

{
  "errorMessage": "java.lang.NullPointerException",
  "errorType": "java.lang.NullPointerException",
  "stackTrace": [
   "com.gns.survey.authserver.login.controller.LoginController.login(LoginController.java:47)",
  "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
  "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
  "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
  "java.lang.reflect.Method.invoke(Method.java:498)"
 ]
}

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

暂无答案!

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

相关问题