我使用 Rest 应用程序创建了 Spring Boot 入门项目。当尝试运行应用程序时 在服务器上运行,它将使用 URL 重定向到 Web 浏览器,直到上下文路径,但在添加端点和 执行它会抛出资源不可用错误。我尝试了很多但没有任何帮助。请帮我解决这个问题。
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 端点:
http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/getCurrencyValue/from/USD/to/INR
http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/wish
面临 HTTP 状态 404 – 未找到错误。请求的资源[/1.SpringRest-WebclientProducerApp/api/currency/wish]不可用
请采取必要措施来解决此案。
您认为为什么 URL 必须包含这一部分?
1.SpringRest-WebclientProducerApp
试试这个
http://localhost:8081/api/currency/wish