SpringBoot控制器未映射

时间:2019-11-23 13:38:47

标签: java spring spring-boot model-view-controller controller

我正在尝试使用一个名为/ movies的端点创建一个基本的SpringBoot Web应用程序。我关注了网站上的一些文档以及网站上的基本YouTube教程。

当我从命令行运行 curl localhost:8080 时,该应用程序返回“有效” ,但是当我尝试访问端点/电影时,我看到“在此服务器上找不到请求的URL /电影。”

主类:

@SpringBootApplication
public class Main 
{
    public static void main( String[] args )
    {
       SpringApplication.run(Main.class, args);
    }
}

控制器类:

@RestController
@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MovieService movie_service;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Movie> getAllMovies(){
        return movie_service.getAllMovies();
    }
}

POM.xml

<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</groupId>
  <artifactId>MovieDCU</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

对于该站点上发布的类似问题,我已经尝试了几乎所有建议的答案,但似乎没有任何解决方法。一件不寻常的事是我在命令行中运行应用程序时看不到映射:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.1.RELEASE)

2019-11-23 13:27:19.243  INFO 30238 --- [           main] com.Main                                 : Starting Main on Conors-MB with PID 30238 (/Users/eclipse-workspace/Movie/target/classes started by x in /Users/eclipse-workspace/Movie)
2019-11-23 13:27:19.249  INFO 30238 --- [           main] com.Main                                 : No active profile set, falling back to default profiles: default
2019-11-23 13:27:20.011  INFO 30238 --- [           main] com.Main                                 : Started Main in 1.209 seconds (JVM running for 1.611)

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试像这样更改班级。 只需在方法级别使用GetMapping。还要注意,我已将您的服务重命名为标准Java命名约定(camelCase)。

@RestController
public class MovieController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/movies")
    public Collection<Movie> getAllMovies(){
        return movieService.getAllMovies();
    }
}

答案 1 :(得分:0)

如果您使用的是Linux计算机,则可以启动默认的Apache服务器。