title: Spring MVC responsebody RestTemplate Connection refused localhost
body: <p>RestTemplate is not working and throwing Connection refused exception. Not sure what's the issue on my personal computer where i use wireless internet and there's no proxy involved.</p>
  
  <p>Junit test</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:testContext.xml")
  public class RestServiceTest {
  
      @Autowired
      private RestTemplate restTemplate;
  
      @Test
      public void testRestService() {
          ResponseEntity&lt;String&gt; response = restTemplate.getForEntity("http://localhost:8080/hello/world", String.class);
  
          String responseTask = response.getBody();
      }
  }
  </code></pre>
  
  <p>test-context.xml</p>
  
  <pre><code>&lt;context:component-scan base-package="com.example" /&gt;
  
  &lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@Controller
  public class SampleController {
  
      @RequestMapping(value = "/hello/{message}", method = RequestMethod.GET)
      public @ResponseBody String sayHello(@PathVariable String message) {
          return "Hello "+message;
      }
  }
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;junit.version&gt;4.11&lt;/junit.version&gt;
  &lt;spring.version&gt;4.0.0.RELEASE&lt;/spring.version&gt;
  &lt;jackson.version&gt;2.3.2&lt;/jackson.version&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
      &lt;version&gt;${spring.version}&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
      &lt;version&gt;${jackson.version}&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
      &lt;version&gt;${spring.version}&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;${junit.version}&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>exception</p>
  
  <pre><code>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
  </code></pre>