0

I have some code that is trying to Autowire a Bean that contains the Spring Boot class RestTemplate.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class JWTAuthenticationFilter extends GenericFilterBean{

    @Autowired
    RequestPublicKey requestPublicKey;

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
                throws IOException, ServletException {

            System.out.println(requestPublicKey.getPublicKey());
            filterChain.doFilter(request, response);

        }
}

My initial code for the class RequestPublicKey looks like this:

@Component
public class RequestPublicKey {

    public String getPublicKey(){
        RestTemplate restTemplate = new RestTemplate();
        String publicCreds = restTemplate.getForObject("http://localhost:8081/get-public-key?kid=bf3b7429-261e-48c5-8409-79e1c9f203de", String.class);
        return publicCreds;
    }
}

When I try to Autowire RequestPublicKey it returns null. I think that is due to that I instantiate RestTemplate with RestTemplate restTemplate = new RestTemplate(). To get around that issue I tried to Autowire RestTemplate like this

@Component
public class RequestPublicKey {

    @Autowired
    private RestTemplate restTemplate;

    public String getPublicKey(){
        String publicCreds = restTemplate.getForObject("http://localhost:8081/get-public-key?kid=bf3b7429-261e-48c5-8409-79e1c9f203de", String.class);
        return publicCreds;
    }
} 

But then I get the Could not autowire. No beans of type RestTemplate found.

Is it possible to Autowire Spring Boot classes? Or should I just instantiate RequestPublicKey like RequestPublicKey requestPublicKey = new RequestPublicKey()?

g3blv
  • 3,877
  • 7
  • 38
  • 51
  • have you defined `RestTemplate` bean in config? I suspect you have not – Arjun Apr 12 '17 at 06:05
  • @Mr.Arjun. No haven't defined it in config. Do you have an example of what that would look like? Sorry I've just started learning Spring Boot and Spring. – g3blv Apr 12 '17 at 06:12
  • could you just edit question and add config code there – Arjun Apr 12 '17 at 06:13
  • there is no need to make `RestTemplate` a spring bean. But you are saying if you autowire `RequestPublicKey` anywhere it is null. This should not be the case. can you show the class where `RequestPublicKey` is autowired. – Patrick Apr 12 '17 at 06:45
  • Try to add a constructor on RequestPublicKey – Moshe Arad Apr 12 '17 at 15:09
  • @Patrick I try to Autowire `RequestPublicKey` in `public class JWTAuthenticationFilter` (the first class in the question) that extends `GenericFilterBean`. I now realised that this seems to be where I have my problem since I'm not able to Autowire `RequestPublicKey` from the class `JWTAuthenticationFilter`. – g3blv Apr 13 '17 at 05:57

2 Answers2

0

As already said in the comments, Spring does not provide a RestTemplate for you automatically. So you either have to set up it yourself, like e.g.

@Configuration
public class MyConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Or you use the RestTemplateBuilder instead which is provided by Spring (boot), so can let Spring inject that to your code.

@Component
public class RequestPublicKey {

    private final RestTemplate restTemplate;

    @Autowired
    public RequestPublicKey(RestTemplateBuilder restTemplateBuilder) {
         this.restTemplate = restTemplateBuilder.build();
    }
    ...

This has some benefits like Spring setting up HttpMessageConverters for your, and you may use the RestTemplateCustomizer. See more information on that in the Spring Boot Docs.

Hope this helps.

Ralf Stuckert
  • 2,120
  • 14
  • 17
  • Thanks I will give that a try. I realised now that my problem is earlier when I try to Autowire `RequestPublicKey` from `JWTAuthenticationFilter`. I tried running `RestTemplate` directly in `JWTAuthenticationFilter` and it worked. – g3blv Apr 13 '17 at 06:12
0

I got it working now. I had two issues with Autowiring. First issue was that some extra setup is needed to Autowire from a GenericFilterBean. I found the a way to setup it up here (link to SO) and here (link to SO). The second issue was how to Autowire the RestTemplate. I went with Ralf's solution. Thanks for all the help.

Community
  • 1
  • 1
g3blv
  • 3,877
  • 7
  • 38
  • 51