简体   繁体   English

Rest Controller 在我的 Springboot 中似乎不起作用

[英]Rest Controller not seem to work in my Springboot

I have a Springboot project I created by copying the libraries from mvn repository, not using SPRING INITLZR.我有一个 Springboot 项目,我通过从 mvn 存储库复制库而不使用 SPRING INITLZR 创建。

Anyway Springboot main seems to be working fine.无论如何 Springboot main 似乎工作正常。 It's just that my Rest Controller is not displaying the message I want to.只是我的 Rest Controller 没有显示我想要的消息。

Main.class主类

@SpringBootApplication
public class MovieInfoServiceApplication {
    public static void main(String[] args) throws IOException, GeneralSecurityException, MessagingException {
                
            SpringApplication.run(MovieInfoServiceApplication.class, args); 
        }
}

RestController.class RestController.class

@RestController
@RequestMapping("/")
public class MovieServiceRestController {

        @GetMapping("/hello")
        //@CrossOrigin(origins = "http://localhost:9005")
        public String hello() {
            return "movie-service-api: hello from MovieServiceRestController";  
        }
}

pom.xml 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>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.backend</groupId>
    <artifactId>movie-info-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>movie-info-service</name>
    <description>microservices about movie info</description>

    <properties>
        <java.version>11</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

This is what I got when I access "localhost:9005/hello"这是我访问“localhost:9005/hello”时得到的

在此处输入图片说明

Springboot logs Springboot 日志

2020-10-18 02:18:35.481  INFO 4347 --- [           main] com.gmailapijava.main.GmailAPIJavaMain   : No active profile set, falling back to default profiles: default
2020-10-18 02:18:38.779  INFO 4347 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$e708c1d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-18 02:18:39.142  INFO 4347 --- [           main] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2020-10-18 02:18:40.286  INFO 4347 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9005 (http)
2020-10-18 02:18:40.307  INFO 4347 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-18 02:18:40.307  INFO 4347 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-18 02:18:41.175  INFO 4347 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-18 02:18:41.176  INFO 4347 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5020 ms
2020-10-18 02:18:42.329  INFO 4347 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-18 02:18:43.369  INFO 4347 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9005 (http) with context path ''
2020-10-18 02:18:43.391  INFO 4347 --- [           main] com.gmailapijava.main.GmailAPIJavaMain   : Started GmailAPIJavaMain in 10.504 seconds (JVM running for 14.339)
2020-10-18 02:18:56.903  INFO 4347 --- [nio-9005-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-10-18 02:18:56.905  INFO 4347 --- [nio-9005-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-10-18 02:18:56.919  INFO 4347 --- [nio-9005-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms
SpringApplication.run(GmailAPIJavaMain.class, args); 

should be应该

SpringApplication.run(MovieInfoServiceApplication.class, args);

Also check if the controller package is getting scanned.还要检查是否正在扫描控制器包。

Try it like this像这样试试

@RestController
public class MovieServiceRestController {

        @GetMapping("/hello")
        //@CrossOrigin(origins = "http://localhost:9005")
        public String hello() {
            return "movie-service-api: hello from MovieServiceRestController";  
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM