Spring REST Endpoint: 404 requested resource is not available error

100 Views Asked by At

I created spring boot starter project with Rest application. When trying to run the application as Run on server it will redirect to web browser with URL till context-path but after adding endpoints and execute it will throw resource not available error. I tried out many but nothing helps.

Controller class:

package in.ineuron.controller;

import java.time.LocalDateTime;

import org.springframework.http.HttpStatus;    
import org.springframework.http.HttpStatusCode;    
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import in.ineuron.response.CurrencyResponse;

@RestController
@RequestMapping("/api/currency")
public class CurrencyController {

@GetMapping("/getCurrencyValue/from/{from}/to/{to}")
public ResponseEntity<CurrencyResponse> getCurrencyValue(@PathVariable String from,
        @PathVariable String to){
    CurrencyResponse response=new CurrencyResponse();
    response.setCurrencyId(1);
    response.setCurrencyFrom(from);
    response.setCurrencyTo(to);
    response.setCurrencyValue(82);
    return new ResponseEntity<CurrencyResponse>(response, HttpStatus.OK);   
}

@GetMapping("/wish")
public ResponseEntity<String> wishMessage(){
    LocalDateTime ldt=LocalDateTime.now();
    int hour=ldt.getHour();
    String body=null;
    if(hour<12)
        body="Good Morning";
    else if(hour<16)
        body="Good Noon";
    else if(hour<20)
        body="Good evening";
    else
        body="Good Night";
    return new ResponseEntity<String>(body, HttpStatus.OK);
    
}

}

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 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>3.1.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>in.ineuron</groupId>
    <artifactId>1.SpringRest-WebclientProducerApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>1.SpringRest-WebclientProducerApp</name>
    <description>Webclient ProducerApp</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <project>

URL end points:

  1. http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/getCurrencyValue/from/USD/to/INR

  2. http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/wish

Facing HTTP Status 404 – Not Found error. The requested resource [/1.SpringRest-WebclientProducerApp/api/currency/wish] is not available

1

There are 1 best solutions below

5
v.ladynev On

Add dependency to pom.xml file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>