1

I am trying to build rest service using Spring Boot. I am learning to implement it using the this Spring's guide. I have built the application using STS as a Maven application and the code is exact replica of the guide and when I try to run the application I get the below connection error

connection error

I also tried to run the github code for the same guide provided by Spring and that code also gives same the error as above. I am new to Spring Boot and any help would be appreciated.

Below is my code App.java

    package Greetings.Web_Services;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            System.out.println("App Started");
            SpringApplication.run(App.class, args);
        }
    }

Greetings.java

    package Greetings.Web_Services;

    public class Greetings {

        private final long id;
        private final String content;

        public Greetings(long id, String content){
            this.id = id;
            this.content = content;
        }

        public long getId(){
            return id;
        }

        public String getContent(){
            return content;
        }
    }

GreetingsController.java

    package Greetings.Web_Services;

    import java.util.concurrent.atomic.AtomicLong;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class GreetingsController {
        private static final String template = "Hello, %s!";
        private final AtomicLong counter = new AtomicLong();

        @RequestMapping("/greeting")
        public Greetings greeting(@RequestParam(value="name", defaultValue="World") String name) {
            return new Greetings(counter.incrementAndGet(),
                        String.format(template, name));
        }
    }

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency> -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

    </project>

GitHub link for the project of above code

Thanks!!

Anantha
  • 99
  • 4
  • 13
  • 2
    The code in GitHub works for me. You've said that you code is identical, but it isn't. Your code's in different packages and you've changed the pom too. Why not share all your code in a GitHub repository so that people can run exactly what you're running? – Andy Wilkinson Aug 30 '16 at 19:33
  • @AndyWilkinson thanks for the reply here is the [git](https://github.com/ananthashankar/Rest-Services/tree/master/Greetings) link for the code I've created using guide. The only thing I have changed is package names, class names and added **"spring-web"** dependency in pom.xml as the dependencies of **"org.springframework.web.bind.annotation.*"**. But for the while running Spring's code I didn't change anything again except for adding **"spring-web"** in pom.xml – Anantha Aug 30 '16 at 23:54
  • Do you get the same error on : http://localhost:8080 ? (without the greeting) – Lazar Lazarov Aug 31 '16 at 15:33
  • @lazarov yes the error is same on http://localhost:8080/ as well – Anantha Aug 31 '16 at 21:23

1 Answers1

0

The code in the guide is correct and it was not working earlier for which I had also raised an issue in the GitHub community of the guide. Later when I updated the STS and restarted, it seems to work now. One of the modifier in the community also recommended the same after I explained the scenario. Hope this saves someone's time.

Anantha
  • 99
  • 4
  • 13
  • I know this is an old thread, but as I came across the same problem (and my application didn't just work like that but required some tweaking) I thought I'd [share my findings](http://stackoverflow.com/questions/41744114/restful-service-application-from-tutorial-not-running-in-browser-whitelabel-err/) in the hope that it could help other people too – antobbo Jan 20 '17 at 08:59