0

The following is the code for RideController class

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<Ride> createRide(@RequestBody Ride ride) {
        return null;
    }

    @RequestMapping(value = "/rides", method = RequestMethod.GET)
    public @ResponseBody List<Ride> getRides() {
        return rideService.getRides();
    }

}

And the following class for tests:

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<List<Ride>> ridesResponse = restTemplate.exchange(
                "http://localhost:8080/ride_tracker/rides", HttpMethod.GET,
                null, new ParameterizedTypeReference<List<Ride>>() {
                });
        List<Ride> rides = ridesResponse.getBody();

        for (Ride ride : rides) {
            System.out.println("Ride name: " + ride.getName());
        }
    }
}

While the 2nd test case succeeds, the first one fails with the following stack trace:

    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)

Similar to this and this post, I have tried to add a User-agent header to the request, but continue to get the same errors.

dorado
  • 1,515
  • 1
  • 15
  • 38
  • 1
    403 indicates that you don't have access. You probably use Spring Security which requires a CSRF token or requires you to login (although the latter would result in a 401). – M. Deinum Jun 05 '19 at 10:36
  • But how come the 2nd method works? – dorado Jun 05 '19 at 10:40
  • You don't need a token for a GET request. – M. Deinum Jun 05 '19 at 10:41
  • But the PUT works from Postman. It shows 200 OK status in postman – dorado Jun 05 '19 at 10:41
  • Then you probably need to provide username/password then. Unless you are doing something in Postman that you aren't doing in here. Nonetheless I still suspect something with a token (for which you do something in postman and not in your test). – M. Deinum Jun 05 '19 at 10:45

0 Answers0