<i id='Mxnsa'><tr id='Mxnsa'><dt id='Mxnsa'><q id='Mxnsa'><span id='Mxnsa'><b id='Mxnsa'><form id='Mxnsa'><ins id='Mxnsa'></ins><ul id='Mxnsa'></ul><sub id='Mxnsa'></sub></form><legend id='Mxnsa'></legend><bdo id='Mxnsa'><pre id='Mxnsa'><center id='Mxnsa'></center></pre></bdo></b><th id='Mxnsa'></th></span></q></dt></tr></i><div id='Mxnsa'><tfoot id='Mxnsa'></tfoot><dl id='Mxnsa'><fieldset id='Mxnsa'></fieldset></dl></div>
  • <tfoot id='Mxnsa'></tfoot>
      <bdo id='Mxnsa'></bdo><ul id='Mxnsa'></ul>

        <small id='Mxnsa'></small><noframes id='Mxnsa'>

      1. <legend id='Mxnsa'><style id='Mxnsa'><dir id='Mxnsa'><q id='Mxnsa'></q></dir></style></legend>

      2. 其他包中的@RestController 不起作用

        时间:2023-09-27
        <i id='TzkOo'><tr id='TzkOo'><dt id='TzkOo'><q id='TzkOo'><span id='TzkOo'><b id='TzkOo'><form id='TzkOo'><ins id='TzkOo'></ins><ul id='TzkOo'></ul><sub id='TzkOo'></sub></form><legend id='TzkOo'></legend><bdo id='TzkOo'><pre id='TzkOo'><center id='TzkOo'></center></pre></bdo></b><th id='TzkOo'></th></span></q></dt></tr></i><div id='TzkOo'><tfoot id='TzkOo'></tfoot><dl id='TzkOo'><fieldset id='TzkOo'></fieldset></dl></div>

            <tbody id='TzkOo'></tbody>

            <bdo id='TzkOo'></bdo><ul id='TzkOo'></ul>
            <legend id='TzkOo'><style id='TzkOo'><dir id='TzkOo'><q id='TzkOo'></q></dir></style></legend>
              • <tfoot id='TzkOo'></tfoot>

                  <small id='TzkOo'></small><noframes id='TzkOo'>

                  本文介绍了其他包中的@RestController 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我从学习 Spring 开始,我创建了创建数据库、插入值并接下来在 Web 浏览器中打印它的基本项目.我的问题是,当我将 RestController 与主类一样放在同一个包中时 - 它可以,但我想将它分发到其他包,并且当我创建新包时,移动 RestController 它不起作用.让我们来解释一下:

                  I start with learning Spring and I create basic project which creates database, insert values and next print it in web browser. My problem is that when I have RestController in the same package like main class - its OK, but I want distribute it to other package and when I create new package, move the RestController it doesn't work. Let met explain:

                  我的项目如下:

                            |-Springtestv_01
                              |-src/main/java
                                |--com.person <-- it's a main package
                                   |-Main.java
                                   |-Person.java
                                   |-PersonLineRunner.java
                                   |-PersonRepository.java
                                   |-PersonController.java
                                |-com.controller <-- second package, I want put here PersonController.java
                              |-src/main/resources
                                |-data.sql
                              pom.xml
                  

                  我的控制器看起来:

                  @RestController
                  public class PersonController {
                  
                      @Autowired PersonRepository personRepository;
                  
                      @RequestMapping("/persons")
                      Collection<Person> persons(){
                          return this.personRepository.findAll();
                      }
                  }  
                  

                  当一切都在 com.person 包中时,我在网络浏览器 http://localhost:8080/persons 并且它工作正常......但我想将 PersonController.java 移动到 com.controller 包,当我移动它时,我的浏览器会打电话给我

                  When everything is in com.person package, I write in web brower http://localhost:8080/persons and it works correctly... But I Want move PersonController.java to com.controller package, and when I moved it, my webbrowers calls me

                  出现意外错误(类型=未找到,状态=404).没有消息可用

                  There was an unexpected error (type=Not Found, status=404). No message available

                  我不知道我应该做什么来修复它.也许我应该在我的 pom.xml 中更改一些内容??

                  and I have no idea what I should do to repair it. Maybe I should change something in my pom.xml ??

                  我的 pom.xml 看起来像

                  My pom.xml looks like

                  <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                      <modelVersion>4.0.0</modelVersion>
                  
                      <groupId>com.person</groupId>
                      <artifactId>person</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
                      <packaging>jar</packaging>
                  
                      <name>SpringTest_v0_1</name>
                      <description>Demo project for Spring Boot</description>
                  
                      <parent>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-parent</artifactId>
                          <version>1.3.0.BUILD-SNAPSHOT</version>
                          <relativePath/> <!-- lookup parent from repository -->
                      </parent>
                  
                      <properties>
                          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                          <java.version>1.8</java.version>
                      </properties>
                  
                      <dependencies>
                          <dependency>
                              <groupId>com.h2database</groupId><artifactId>h2</artifactId>
                          </dependency>
                  
                          <dependency>
                              <groupId>org.springframework.boot</groupId>
                              <artifactId>spring-boot-starter-web</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-data-jpa</artifactId>
                          </dependency>
                          <dependency>
                              <groupId>org.springframework.boot</groupId>
                              <artifactId>
                                  spring-boot-starter-data-elasticsearch
                              </artifactId>
                          </dependency>
                      </dependencies>
                  
                      <build>
                          <plugins>
                              <plugin>
                                  <groupId>org.springframework.boot</groupId>
                                  <artifactId>spring-boot-maven-plugin</artifactId>
                              </plugin>
                          </plugins>
                      </build>
                  
                      <repositories>
                          <repository>
                              <id>spring-snapshots</id>
                              <name>Spring Snapshots</name>
                              <url>https://repo.spring.io/snapshot</url>
                              <snapshots>
                                  <enabled>true</enabled>
                              </snapshots>
                          </repository>
                          <repository>
                              <id>spring-milestones</id>
                              <name>Spring Milestones</name>
                              <url>https://repo.spring.io/milestone</url>
                              <snapshots>
                                  <enabled>false</enabled>
                              </snapshots>
                          </repository>
                      </repositories>
                      <pluginRepositories>
                          <pluginRepository>
                              <id>spring-snapshots</id>
                              <name>Spring Snapshots</name>
                              <url>https://repo.spring.io/snapshot</url>
                              <snapshots>
                                  <enabled>true</enabled>
                              </snapshots>
                          </pluginRepository>
                          <pluginRepository>
                              <id>spring-milestones</id>
                              <name>Spring Milestones</name>
                              <url>https://repo.spring.io/milestone</url>
                              <snapshots>
                                  <enabled>false</enabled>
                              </snapshots>
                          </pluginRepository>
                      </pluginRepositories>
                  
                  </project>
                  

                  它是自动生成的,我只写了一个依赖

                  It is generated automatically, I write only one dependency

                      <dependency>
                          <groupId>com.h2database</groupId><artifactId>h2</artifactId>
                      </dependency>
                  

                  推荐答案

                  使用basePackages:

                  Use basePackages:

                  @ComponentScan(basePackages = { "com.person","com.controller"} )
                  

                  这篇关于其他包中的@RestController 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Java 中弃用包的最清晰方法是什么? 下一篇:R:无法安装 rJava;什么是 r-api-3.4?

                  相关文章

                  最新文章

                1. <legend id='1fiSb'><style id='1fiSb'><dir id='1fiSb'><q id='1fiSb'></q></dir></style></legend>
                2. <tfoot id='1fiSb'></tfoot>

                3. <i id='1fiSb'><tr id='1fiSb'><dt id='1fiSb'><q id='1fiSb'><span id='1fiSb'><b id='1fiSb'><form id='1fiSb'><ins id='1fiSb'></ins><ul id='1fiSb'></ul><sub id='1fiSb'></sub></form><legend id='1fiSb'></legend><bdo id='1fiSb'><pre id='1fiSb'><center id='1fiSb'></center></pre></bdo></b><th id='1fiSb'></th></span></q></dt></tr></i><div id='1fiSb'><tfoot id='1fiSb'></tfoot><dl id='1fiSb'><fieldset id='1fiSb'></fieldset></dl></div>

                    <small id='1fiSb'></small><noframes id='1fiSb'>

                      • <bdo id='1fiSb'></bdo><ul id='1fiSb'></ul>