Spring MVC responsebody RestTemplate Connection拒绝localhost

时间:2014-08-27 02:48:20

标签: java rest spring-mvc junit

RestTemplate不起作用并且抛出Connection拒绝异常。我不确定在我的个人电脑上使用无线互联网的问题是什么,并且没有涉及代理。

Junit测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:testContext.xml")
public class RestServiceTest {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void testRestService() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/hello/world", String.class);

        String responseTask = response.getBody();
    }
}

测试context.xml中

<context:component-scan base-package="com.example" />

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>

控制器

@Controller
public class SampleController {

    @RequestMapping(value = "/hello/{message}", method = RequestMethod.GET)
    public @ResponseBody String sayHello(@PathVariable String message) {
        return "Hello "+message;
    }
}

的pom.xml

<junit.version>4.11</junit.version>
<spring.version>4.0.0.RELEASE</spring.version>
<jackson.version>2.3.2</jackson.version>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>

例外

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/hello/world":Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

0 个答案:

没有答案