【问题标题】:Getting 404 error with Spring Boot + REST applicationSpring Boot + REST 应用程序出现 404 错误
【发布时间】:2021-12-18 04:43:20
【问题描述】:

基本上我一直在使用 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>

在这里,我可以向您展示我用于其余模块的 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>

【问题讨论】:

  • "@ComponentScan("com.opus.app")" - @RestController 不在此包“下”(它在 com.opus.restApi.implementations 中)。您可以尝试设置@ComponentScan("com.opus") 吗?

标签: java xml spring spring-boot maven


【解决方案1】:

您需要使用其中一个methods 来获取所有活动端点的列表。

这将使您能够查看您的应用公开的端点的完整列表。然后,您要么在列表中看到目标端点,要么看不到。如果您确实看到了它们,这意味着您之前使用了错误的 URI。

如果不是,则表示控制器 bean 尚未添加到应用程序上下文中……这可能是由于 @ComponentScan 注释指向了错误的包……

【讨论】:

    【解决方案2】:

    从您的主要 RestApiApplication 类中删除 @ComponentScan@SpringBootApplication 封装了 @Configuration@EnableAutoConfiguration@ComponentScan 注释及其默认属性。 @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 管理的 bean。

    【讨论】:

    • @Brik000,这有帮助吗?如果有不清楚的地方请告诉我。
    猜你喜欢
    • 2014-10-25
    • 2019-02-20
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2017-09-18
    • 1970-01-01
    • 2015-09-04
    相关资源
    最近更新 更多