基本上我一直在使用 spring 在這個多模塊應用程式中作業,但是當我嘗試使用來自我的 Rest 控制器的服務時,我不斷收到這個 404 錯誤。
{
"timestamp": "2021-11-03T18:54:23.230 00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/opus/auth/client"
}
這是我的應用程式屬性:
server.port=8080
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
spring.jackson.serialization.fail-on-self-references=false
server.servlet.context-path=/api/opus
以下是其余控制器之一的示例:
package com.opus.restApi.implementations;
@RequestMapping("/auth/client")
@RestController
@CrossOrigin(origins = "*")
public class ClientRestController implements IClientRestController{
@Autowired
private IClientService service;
@GetMapping("")
public Iterable<Client> findAll() {
return service.findAll();
}
}
這是我的主要課程:
package com.opus.restApi;
@SpringBootApplication
@ComponentScan("com.opus.app")
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
public class RestApiApplication {
public static void main(String[] args) {
SpringApplication.run(RestApiApplication.class, args);
}
}
我還可以分享我正在使用的主 pom:
<?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.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.opus</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OpusApp</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>logic</module>
<module>model</module>
<module>persistence</module>
<module>restApi</module>
</modules>
</project>
在這里,我可以向您展示我用于 rest 模塊的 pom:
<?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>com.opus</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>restApi</artifactId>
<version>${parent.version}</version>
<name>restApi</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.opus</groupId>
<artifactId>logic</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/webjars-locator-core -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>0.48</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/sockjs-client -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/stomp-websocket -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars.bower/jquery -->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
uj5u.com熱心網友回復:
@ComponentScan從主RestApiApplication類中洗掉。@SpringBootApplication封裝@Configuration,@EnableAutoConfiguration和@ComponentScanannotations 及其默認屬性。的默認值@ComponentScan意味著@ComponentScan掃描使用的包上的所有子包。這就是為什么在專案的基本包中包含主類通常是一個好習慣的原因。
package com.opus.restApi;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
public class RestApiApplication {
public static void main(String[] args) {
SpringApplication.run(RestApiApplication.class, args);
}
}
這應該解決似乎與您的ClientRestController類沒有被掃描相關的問題,因此沒有創建相應的 Spring-managed bean。
uj5u.com熱心網友回復:
您需要使用這些方法之一來獲取所有活動端點的串列。
這將使您能夠查看應用程式公開的端點的完整串列。然后,您要么在串列中看到目標端點,要么看不到。如果您確實看到了它們,則意味著您之前使用了錯誤的 URI。
如果沒有,則表示控制器 bean 尚未添加到應用程式背景關系中……這可能是由于@ComponentScan注釋指向錯誤的包……
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/347458.html
上一篇:如何從Access資料庫VB.net中檢索特定資料?
下一篇:函式模板特化,不改變原型
