I am trying to implement Spring Boot RestTemplate and invoke external REST service using Java Code. This is working as java application but when I am deploying same code in docker container I am getting error
org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [error code: 1010]
Here is my code ..which is running standalone ..
@RequestMapping(value = "/extservice",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET)
public User httpcall() {
ResponseEntity<User> result = null;
try {
HttpHeaders headers = new HttpHeaders();
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<Object>(headers);
final String uri = "https://reqres.in/api/users/2";
RestTemplate restTemplate = new RestTemplate();
result = restTemplate.getForEntity(uri, User.class);
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return result.getBody();
}
and here is the error stack trace
org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [error code: 1010]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:342)
at com.dockerforjavadevelopers.hello.HelloController.httpcall(HelloController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....
2020-07-25 04:52:17.578 ERROR 1 --- [nio-9898-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/opa-test] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.dockerforjavadevelopers.hello.HelloController.httpcall(HelloController.java:48) ~[classes!/:0.1.0]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
Here is my docker file
# Maven build container
FROM maven:3.6.3-jdk-11-slim as mavenbuild
COPY pom.xml /tmp/
COPY src /tmp/src/
WORKDIR /tmp/
RUN mvn package
#pull base image
FROM openjdk:8-jdk-alpine
EXPOSE 9898
#default command
CMD java -jar /data/opa-test-0.1.0.jar
#copy hello world to docker image from builder image
COPY --from=mavenbuild /tmp/target/opa-test-0.1.0.jar /data/opa-test-0.1.0.jar