HttpClientErrorException:403在Spring应用程序中被禁止

时间:2019-06-05 10:21:17

标签: java spring java-ee

以下是RideController类的代码

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();
    }

}

以及以下用于测试的类:

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());
        }
    }
}

第二个测试用例成功时,第一个失败并显示以下堆栈跟踪:

    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)

类似于thisthis的帖子,我试图将User-agent标头添加到请求中,但是继续出现相同的错误。

0 个答案:

没有答案