3

I was trying my hand at writing a sample Spring Boot application.

My initial thought was it's an easy example to follow and write but I am not able to print a simple statement upon hitting my endpoint.

I don't want to use any view (HTML,JSP etc) right now as I want to learn the backend first.

I have followed:

However, still stuck after a whole day of googling.

So let me reiterate the steps, I did.

  1. Initialized a spring boot project from spring.io and downloaded zip.
  2. Extracted the zip and imported it into Eclipse.
  3. Configured a Rest Controller with an endpoint to just print a hello world statement.
  4. Tomcat 9.0.56 is present on my system.
  5. Running TodoApplication as Java application or Spring boot.
  6. URL I am hitting is http://localhost:8080/greet
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @GetMapping("/greet")
    public String sayHelloWorld() {
        return "Hello World";
    }
}

Spring Boot Application:

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

@SpringBootApplication
public class TodoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TodoApplication.class, args);
    }

}

pom file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sample</groupId>
    <artifactId>todo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>todo</name>
    <description>Back-end for To DO App</description>

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

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

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

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

After navigating greet URL:

URL not present error

Got the following exception at console:

Spring boot Circular view error

catch23
  • 17,519
  • 42
  • 144
  • 217
Harsh Sengar
  • 95
  • 1
  • 9
  • when you deploy to a standalone tomcat, you should know the context path! (What is the name of the ".war" ? Why you didn't post the brwoser url? (which you issued in browser, which would be a *crucial detail* regarding this "issue"!?) – xerx593 Jan 14 '22 at 13:49
  • please additionally "do": https://spring.io/blog/2014/03/07/deploying-spring-boot-applications#what-about-the-java-ee-application-server respectively "study": https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.traditional-deployment – xerx593 Jan 14 '22 at 13:54
  • the 404 (on `/error` ..circular) seems to be a "follow-up symptom" ...I am missing a `WebApplicationInitializer`/`SpringBootServletInitializer`! ;) – xerx593 Jan 14 '22 at 13:57
  • I "studied" for us! :) Actually we need only: 1. `war` (or its gradle variant) 2. A(n auto-scanned) ServletInitilizer like [here](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.6.2&packaging=war&jvmVersion=17&groupId=com.example&artifactId=war-app&name=war-app&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.warapp&dependencies=web) 3. Deploy "myApp.war" to standalone tomcat (little wait, watch logs) 4. Issue `[protocol://][:server_port]/myApp/greet` from browser/curl/postman . – xerx593 Jan 14 '22 at 14:05
  • note the "packaging" option of spring-starter! And important (to know): in embedded/default config "context root" is at: `http://localhost:8080/` ..in a standalone container (tomcat) it would be: `http://localhost:8080//` (in maven `full_name_of_war_file_without_dot` would "default to" `${build.finalName}`) – xerx593 Jan 14 '22 at 14:10
  • @xerx593 My doubt is that I am not packaging my solution to be a war or jar, I just want to have an endpoint running to which I can call and output something on browser or curl response. Please enlighten me if I sound naive. Thanks – Harsh Sengar Jan 14 '22 at 14:28
  • [start with this](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.6.2&packaging=war&jvmVersion=17&groupId=com.example&artifactId=war-app&name=war-app&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.warapp&dependencies=web) (your name, your company...) – xerx593 Jan 14 '22 at 14:31
  • 1
    ..add your controller to it (in the correct package - same or child package of SpringBootApplication). – xerx593 Jan 14 '22 at 14:32
  • 1
    to tun embedded: `mvn spring-boot:run`, then greeting will be at `http://localhost:8080/greet` . – xerx593 Jan 14 '22 at 14:35
  • 1
    to tun standalone: 1.`mvn clean package` 2. copy war file to `$CATALINA_HOME/webapps` 3. wait/watch logs 4. `http://localhost:8080//greet` – xerx593 Jan 14 '22 at 14:36
  • @xerx593 So the good news is that finally Hello World is returned. I have two queries: 1) Why do I have to keep the controller class in same package as SpringBootApplication ? 2) How do I run my endpoint without Apache Tomcat ? – Harsh Sengar Jan 14 '22 at 15:10
  • 1. https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code – xerx593 Jan 14 '22 at 15:25
  • 2. `[mvn|gradle] spring-boot:run` ;) – xerx593 Jan 14 '22 at 15:26
  • 1
    @xerx593 Thanks a lot for the help. Now I can play around more to understand Spring world. – Harsh Sengar Jan 14 '22 at 15:44

1 Answers1

2

So the solution as pointed out by xerx593 is below:

  1. Make sure to select packaging as war/jar in https://start.spring.io/.
  2. Import the zip file.
  3. Add your controller class in same or child package of SpringBootApplication, refer this for more info https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code.
  4. Open terminal and change directory to working folder.
  5. Run mvn spring-boot:run if using maven.
  6. Hit the endpoint for example http://localhost:8080/greet or curl request.

Also I have noticed that 8080 port is listening but spring boot fails to register on it, in this case set

server.port = port other than 8080 

in application.properties.

8080 listening

application properties file

Harsh Sengar
  • 95
  • 1
  • 9