title: HttpClientErrorException: 403 Forbidden in spring application
body: <p>The following is the code for <code>RideController</code> class</p>
  
  <pre><code>import java.util.List;
  import org.springframework.*;
  import com.dorado.model.Ride;
  import com.dorado.service.RideService;
  
  @Controller
  public class RideController {
  
      @Autowired
      private RideService rideService;
  
      @RequestMapping(value = "/ride", method = RequestMethod.PUT)
      public @ResponseBody List&lt;Ride&gt; createRide(@RequestBody Ride ride) {
          return null;
      }
  
      @RequestMapping(value = "/rides", method = RequestMethod.GET)
      public @ResponseBody List&lt;Ride&gt; getRides() {
          return rideService.getRides();
      }
  
  }
  </code></pre>
  
  <p>And the following class for tests:</p>
  
  <pre><code>package com.dorado.controller;
  
  import java.util.List;
  
  import org.junit.Test;
  import org.springframework.core.ParameterizedTypeReference;
  import org.springframework.http.HttpEntity;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.HttpMethod;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.client.RestTemplate;
  
  import com.dorado.model.Ride;
  
  public class RestControllerTest {
  
      @Test(timeout=5000)
      public void testCreateRide() {
          RestTemplate restTemplate = new RestTemplate();
  
          Ride ride = new Ride();
          ride.setName("Train Ride");
          ride.setDuration(35);
  
          restTemplate.put("http://localhost:8080/ride_traker/ride", ride);
      }
  
      @Test(timeout=3000)
      public void testGetRides() {
          RestTemplate restTemplate = new RestTemplate();
  
          ResponseEntity&lt;List&lt;Ride&gt;&gt; ridesResponse = restTemplate.exchange(
                  "http://localhost:8080/ride_tracker/rides", HttpMethod.GET,
                  null, new ParameterizedTypeReference&lt;List&lt;Ride&gt;&gt;() {
                  });
          List&lt;Ride&gt; rides = ridesResponse.getBody();
  
          for (Ride ride : rides) {
              System.out.println("Ride name: " + ride.getName());
          }
      }
  }
  </code></pre>
  
  <p>While the 2nd test case succeeds, the first one fails with the following stack trace:</p>
  
  <pre><code>    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
      at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
      at org.springframework.web.client.RestTemplate.put(RestTemplate.java:432)
      at com.pluralsight.controller.RestControllerTest.testCreateRide(RestControllerTest.java:34)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
      at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Similar to <a href="https://stackoverflow.com/questions/44922261/why-do-i-always-get-403-when-fetching-data-with-resttemplate">this</a> and <a href="https://stackoverflow.com/questions/38857598/getting-403-forbidden-error/38859776">this</a> post, I have tried to add a User-agent header to the request, but continue to get the same errors.</p>