Thursday, April 25, 2024
HomeJavaEasy methods to Repair Spring Boot can not entry REST Controller on...

Easy methods to Repair Spring Boot can not entry REST Controller on localhost (404) Error in Java


Hi there guys, in case you are getting “can not entry REST Controller on localhost (404)” error in your Spring Boot utility and questioning tips on how to clear up it then you might have come to the appropriate place. Earlier, I’ve shared tips on how to solved 5 widespread Spring Framework errors and exception and on this article, we are going to check out the spring boot can’t entry relaxation controller on localhost (404) challenge. A 404 Not Discovered Error on your REST Controllers is a comparatively typical drawback that many builders encounter whereas working with Spring Boot purposes. On this Spring boot tutorial, let’s discover why such an error came about, and afterward, we are going to discover ways to repair this challenge.

Drawback Description

Let’s perceive it with the assistance of a situation. We have now a REST API whose goal is to function the cricket match info supplier. At this level we have now just one endpoint /team-info, as proven within the instance under:

REST Controller

bundle com.javarevisited.ipldashboard.controller; 

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.internet.bind.annotation.RequestMapping;

import org.springframework.internet.bind.annotation.RestController;

@RestController

@RequestMapping("/api/v1")

public class MatchController{

@RequestMapping("/team-info")

public ResponseEntity<Group> getTeamInfo(){

Group crew = new Group();

crew.setId(1);

crew.setTeamName("India");

crew.setTotalMatches(30);

crew.setTotalWins(21);

return new ResponseEntity<>(crew, HttpStatus.OK);

}

}

Now, we want one thing to run this utility. You should utilize the next Utility’s major class to launch your Spring Boot utility.

Spring Boot App (Most important utility)

bundle com.javarevisited.ipldashboard.app; 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class IplDashboardApplication {

public static void major(String[] args) {

SpringApplication.run(IplDashboardApplication.class, args);

}

}

Group Class (Mannequin)

bundle com.javarevisited.ipldashboard.mannequin; 

import jakarta.persistence.Entity;

import jakarta.persistence.GeneratedValue;

import jakarta.persistence.GenerationType;

import jakarta.persistence.Id;

import lombok.Knowledge;

@Entity

@Knowledge

public class Group {

@Id

@GeneratedValue(technique = GenerationType.AUTO)

personal lengthy id;

personal String teamName;

personal lengthy totalMatches;

personal lengthy totalWins;

public Group(String teamName, lengthy totalMatches){

this.teamName = teamName;

this.totalMatches = totalMatches;

}

public Group() {

}

@Override

public String toString(){

return this.teamName + " " + this.totalMatches + " " + this.totalWins;

}

}

The crew mannequin class within the above-mentioned code shops crew statistics just like the variety of video games and victories.

Operating the applying

Now it’s time to run our utility and entry the uncovered end-point i.e. /team-info. Sadly, if you attempt to entry the “/team-info ” endpoint it would encounter a 404 error:

{

    "timestamp": "2023-01-03T12:38:22.853+00:00",

    "standing": 404,

    "error": "Not Discovered",

    "message": "No message out there",

    "path": "/api/v1/team-info"

}

Easy methods to clear up this drawback?

Initially, it is advisable to know the place to search for doable points. Right here we have to consider two courses (i.e. IplDashboardApplication and MatchController) 

We begin with an annotation is written on high of sophistication IplDashboardApplication i.e. @SpringBootApplication annotation to understand the issue. Truly, this annotation is a synthesis of three annotations: 

@Configuration

@ComponentScan

@EnableAutoConfiguration

With none further inputs, the default @ComponentScan annotation instructs Spring to scan the present bundle and all of its youngster packages.

Keep in mind: Our major utility resides in bundle (com.javarevisited.ipldashboard.app) whereas the controller is positioned below (com.javarevisited.ipldashboard.controller) the bundle.

The courses within the bundle com.javarevisited.ipldashboard.app and below it would thus be the one ones scanned. However, our controller class MatchController resides in a special bundle com.javarevisited.ipldashboard.controller which is neither within the utility’s bundle nor in its sub bundle. 

We understood the issue that, the applying is unable to seek out the REST controller. Due to this fact it throws can’t entry the REST Controller on localhost (404). Now it’s time to unravel the issue. There are doable two methods to repair this challenge.

1. Place the Utility below the basis folder

The Most important Utility class IplDashboardApplication must be positioned within the undertaking’s root folder, as this is a wonderful follow. On this method, it would robotically seek for the Controller class that’s included in a sub-package. 

Right here is an efficient bundle construction as illustrated under:

As you may see from the above picture we have now taken out the IplDashboardApplication class and positioned it on the root bundle com.javarevisited.ipldashboard, therefore it may well simply discover the courses from different packages. The up to date code script will be seen under.

Spring Boot App (Most important utility)

bundle com.javarevisited.ipldashboard; 



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;





@SpringBootApplication

public class IplDashboardApplication {



   public static void major(String[] args) {

      SpringApplication.run(IplDashboardApplication.class, args);

   }



}

REST Controller

bundle com.javarevisited.ipldashboard.controller; 



import com.javarevisited.ipldashboard.mannequin.Group;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.internet.bind.annotation.RequestMapping;

import org.springframework.internet.bind.annotation.RestController;



@RestController

@RequestMapping("/api/v1")

public class MatchController{



    @RequestMapping("/team-info")

    public ResponseEntity<Group> getTeamInfo(){



        Group crew = new Group();

        crew.setId(1);

        crew.setTeamName("India");

        crew.setTotalMatches(30);

        crew.setTotalWins(21);



        return new ResponseEntity<>(crew, HttpStatus.OK);

    }

}

GET request to /api/v1/team-info will return following info.

{

    "id": 1,

    "teamName": "India",

    "totalMatches": 30,

    "totalWins": 21

}

Group Class (Mannequin)

bundle com.javarevisited.ipldashboard.mannequin;



import jakarta.persistence.Entity;

import jakarta.persistence.GeneratedValue;

import jakarta.persistence.GenerationType;

import jakarta.persistence.Id;

import lombok.Knowledge;



@Entity

@Knowledge

public class Group {



    @Id

    @GeneratedValue(technique = GenerationType.AUTO)

    personal lengthy id;

    personal String teamName;

    personal lengthy totalMatches;

    personal lengthy totalWins;



    public Group(String teamName, lengthy totalMatches){

        this.teamName = teamName;

        this.totalMatches = totalMatches;

    }



    public Group() {



    }



    @Override

    public String toString(){

        return this.teamName + " " + this.totalMatches + " " + this.totalWins;

    }

}

2. Add Specific Scan (@ComponentScan)

One other selection is to incorporate the @ComponentScan annotation and inform it explicitly the packages to scan as a starting level for the applying.

@ComponentScan(basePackages = "com.javarevisited.ipldashboard.controller")

By including the above annotation we are able to inform our utility to scan the courses residing contained in the com.javarevisited.ipldashboard.controller bundle. By doing this our utility will be capable of learn MatchController thus, this additionally resolves the problem. Under is the up to date code script.

Spring Boot App (Most important utility)

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;



@SpringBootApplication

@ComponentScan(basePackages = "com.javarevisited.ipldashboard.controller")

public class IplDashboardApplication {



   public static void major(String[] args) {

      SpringApplication.run(IplDashboardApplication.class, args);

   }



}

REST Controller

bundle com.javarevisited.ipldashboard.controller;



import com.javarevisited.ipldashboard.mannequin.Group;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.internet.bind.annotation.RequestMapping;

import org.springframework.internet.bind.annotation.RestController;



@RestController

@RequestMapping("/api/v1")

public class MatchController{



    @RequestMapping("/team-info")

    public ResponseEntity<Group> getTeamInfo(){



        Group crew = new Group();

        crew.setId(1);

        crew.setTeamName("India");

        crew.setTotalMatches(30);

        crew.setTotalWins(21);



        return new ResponseEntity<>(crew, HttpStatus.OK);

    }

}

Group Class (Mannequin)

bundle com.javarevisited.ipldashboard.mannequin;



import jakarta.persistence.Entity;

import jakarta.persistence.GeneratedValue;

import jakarta.persistence.GenerationType;

import jakarta.persistence.Id;

import lombok.Knowledge;



@Entity

@Knowledge

public class Group {



    @Id

    @GeneratedValue(technique = GenerationType.AUTO)

    personal lengthy id;

    personal String teamName;

    personal lengthy totalMatches;

    personal lengthy totalWins;



    public Group(String teamName, lengthy totalMatches){

        this.teamName = teamName;

        this.totalMatches = totalMatches;

    }



    public Group() {



    }



    @Override

    public String toString(){

        return this.teamName + " " + this.totalMatches + " " + this.totalWins;

    }

}

Now if you happen to make a GET request to /api/v1/team-info you’ll get the crew’s info as seen under.

{

    "id": 1,

    "teamName": "India",

    "totalMatches": 30,

    "totalWins": 21

}

Conclusion

On this article, we be taught tips on how to repair a typical drawback that almost all developer encounter whereas working with Spring Boot utility and that’s Spring Boot can’t discover REST Controller on localhost (404). We began our dialogue with a easy situation to know why such an issue came about at first after which we dig deep into its two doable options. 

The primary one is putting your spring boot utility below the basis bundle so that it’s going to scan for elements in packages under com.javarevisited.ipldashboard and the second is to scan the controller bundle explicitly by including @ComponentScan annotation. I hope the mentioned answer lets you repair the issue.  

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments