-1

I am trying to read properties from a file with Spring. However I am constantly get NullPointerExceptions. I am not sure what I do wrong since I already have done this in my RestController class with an @Configuration annotation.

@Component
@PropertySource("classpath:recaptcha.properties")
public class RecaptchaProvider {

  @Value("${com.xxx.user.recaptcha.verify.secret}")
  private String secret;

  @Value("${com.xxx.user.recaptcha.verify.url}")
  private String serviceUrl;

  public RecaptchaProvider() {
    //do nothing
  }

  public String getSecret() {
    return secret;
  }

  public String getServiceUrl() {
    return serviceUrl;
  }
}

And my caller class:

@Component
public class RecaptchaServiceImpl implements RecaptchaService {

  @Autowired
  RecaptchaProvider provider;

  @Override
  public boolean isCaptchaValid(String response) {
    String secret = provider.getSecret(); // Nullpointer here
    String serviceUrl = provider.getServiceUrl();

    CaptchaValidation captchaValidator = new CaptchaValidation();
    ReCaptchaVerfiyResponse result =
        captchaValidator.postForCaptchaValidation(secret, response, serviceUrl);

    return Boolean.valueOf(result.getSuccess());
  }
}

The project set-up:

enter image description here

and inside my property file:

com.xxx.user.recaptcha.verify.url=https://www.google.com/recaptcha/api/siteverify
com.xxx.user.recaptcha.verify.secret=xxxxxxxxxxxx

The exception:

java.lang.NullPointerException
    at com.xxx.user.recaptcha.impl.RecaptchaServiceImpl.isCaptchaValid(RecaptchaServiceImpl.java:24)
    at com.xxx.rest.user.impl.UserController.checkReCaptcha(UserController.java:135)
    at com.xxx.rest.user.impl.UserController.addUser(UserController.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
dur
  • 15,689
  • 25
  • 79
  • 125
Maevy
  • 261
  • 4
  • 24
  • 1
    Let me guess in your `UserController` you do `new RecaptchaServiceImpl()`. Also `@PropertySource` should go on a `@Configuration` class else it won't do much. – M. Deinum Nov 23 '16 at 14:36
  • 1
    Yes i do RecaptchaServiceImpl captchaservice = new RecaptchaServiceImpl(); return captchaservice.isCaptchaValid(captchaResponse); – Maevy Nov 23 '16 at 14:38

1 Answers1

0

Ok thanks to M.Deinum "guess" I fixed the issue. I didn't realized that I mixed a bean with a component and forgot that a "new" messes up spring lfc

Now it looks like this

@Component
public class RecaptchaProvider {

  private String secret;

  private String serviceUrl;


  public RecaptchaProvider(String secret, String serviceUrl) {
    this.secret = secret;
    this.serviceUrl = serviceUrl;
  }

  public String getSecret() {
    return secret;
  }

  public String getServiceUrl() {
    return serviceUrl;
  }
}

The property Provider

@Configuration
@PropertySource("classpath:recaptcha.properties")
public class RecaptchaConfig {

  @Value("${com.xxx.user.recaptcha.verify.secret}")
  private String secret;

  @Value("${com.xxx.user.recaptcha.verify.url}")
  private String serviceUrl;

  @Bean
  public RecaptchaProvider getRecaptchaConfig() {
    return new RecaptchaProvider(secret,serviceUrl);
  }
}

The Service

  @Autowired
  RecaptchaConfig config;

  @Override
  public boolean isCaptchaValid(String response) {
    RecaptchaProvider provider = config.getRecaptchaConfig();
    String secret = provider.getSecret();
      String serviceUrl = provider.getServiceUrl();
      CaptchaValidation captchaValidator = new CaptchaValidation();
      ReCaptchaVerfiyResponse result =
          captchaValidator.postForCaptchaValidation(secret, response, serviceUrl);

      return Boolean.valueOf(result.getSuccess());
  }
}

And finally the method in my Controller

  @Autowired
  protected RecaptchaServiceImpl recaptchaService;

  private boolean checkReCaptcha(String captchaResponse){
    return recaptchaService.isCaptchaValid(captchaResponse);
  }
Maevy
  • 261
  • 4
  • 24