-1

I am new to springboot. I have a controller class like below,

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @RequestMapping("/hello")
    public String getHi() {
        return "Hi";
    }
}

<?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.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>16</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>

Console,

Starting service [Tomcat]
Starting Servlet engine: [Apache Tomcat/9.0.46]
Initializing Spring embedded WebApplicationContextRoot WebApplicationContext: initialization completed in 626 ms
Tomcat started on port(s): 8080 (http) with context path ''

After running the project I tried opening "localhost:8080/hello" in a browser but I'm getting localhost connection refused. In console, I can able to see that, Tomcat server is started with port 8080. Thanks in Advance.

UPDATE I resolved my issue by adding below line in my Main class,

@Autowired
    Controller controller;
Kousalya
  • 700
  • 10
  • 29
  • add `@GET` on next to the `@RequestMapping`. – randytan Jun 09 '21 at 04:47
  • @randytan I have tried like this, ```@RequestMapping (value ="/hello",method = RequestMethod.GET)``` but no change – Kousalya Jun 09 '21 at 05:12
  • Check to see if the port `8080` is not used by another application. Or add `server.port=8082` to test your application on another port with this url : `localhost:8082/hello` – Harry Coder Jun 09 '21 at 05:29
  • @HarryCoder I tried with 8082 port. this time I got "whitelabel error page" – Kousalya Jun 09 '21 at 05:37
  • What is th HTTP status error code? – Harry Coder Jun 09 '21 at 05:41
  • @HarryCoder ```Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Jun 09 11:10:40 IST 2021 There was an unexpected error (type=Not Found, status=404).``` – Kousalya Jun 09 '21 at 05:42
  • 1
    This link could answer your question : https://stackoverflow.com/questions/49034254/spring-boot-whitelabel-error-page-type-not-found-status-404 – Harry Coder Jun 09 '21 at 05:46
  • Ok - one you need to put the RequestMethod.GET, second you need to package the Main Application class properly. Your main class must be on the ROOT of your project. Not inside some other packages. @Kousalya. Ok - about the port, did you package your war/jar and deploy to tomcat or you start your tomcat and IDE together? – randytan Jun 09 '21 at 06:21
  • @randytan thank you for helping out. Now I resolved my issue – Kousalya Jun 09 '21 at 06:57
  • @HarryCoder thank you for helping out. Now I resolved my issue – Kousalya Jun 09 '21 at 06:58
  • Also I have a doubt. Why needs to add the controller class first to work? Any other way can I avoid this? Because I dont understand the logic behind that. – Kousalya Jun 09 '21 at 07:04
  • @kousalya what do you mean with adding the controller class? – randytan Jun 09 '21 at 07:53
  • @randytan I mean, adding it inside the package where the main class present. – Kousalya Jun 09 '21 at 10:06
  • @randytan previous I had a seperate package for controller. – Kousalya Jun 09 '21 at 10:07
  • @Kousalya you can put your controller (by specifying `@Controller` or `@RestController`) in any package. SpringBoot will automatically scan all of beans when it starts. – randytan Jun 10 '21 at 05:32

2 Answers2

0

Check if your application.yaml contains

server:
  port: 8080

And you use correct port in http client.

Oleg Poltoratskii
  • 667
  • 11
  • 10
-2

Try accessing 127.0.0.1:8080/hello to ensure that the loopback is valid

DWang
  • 1
  • 1