java - Spring Boot 应用程序未在 Tomcat 中配置的端口号上启动

标签 java spring spring-boot

我正在 spring 工具套件工具中运行示例 spring boot 应用程序。 配置端口后,我无法从浏览器启动应用程序。我收到 404 未找到错误。 Spring boot在tomcat上正常运行。

应用程序属性

hello.greeting=很高兴见到你

服务器端口=9874

有人可以帮我解决这个问题吗?

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloBootApplication {

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


package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloProperties props;

    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
        return props.getGreeting()+name;
    }

}

package demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("hello")
public class HelloProperties {
    private String greeting = "Welcome ";

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
}

2018-07-22 17:17:32.798  INFO 11824 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-22 17:17:32.952  INFO 11824 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-22 17:17:33.000  INFO 11824 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9874 (http) with context path ''
2018-07-22 17:17:33.006  INFO 11824 --- [           main] demo.HelloBootApplication                : Started HelloBootApplication in 2.083 seconds (JVM running for 2.862)

这是 Spring Boot 应用程序,在下面的链接上收到 404 Not Found http://localhost:9874/

最佳答案

您的网址错误。您必须使用 RequestParam 名称调用 url。

使用此网址http://localhost:9874/hello?name=test

关于java - Spring Boot 应用程序未在 Tomcat 中配置的端口号上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464453/

相关文章:

java - put(String, List<String>) 方法不适用

java - Spring 和 hibernate.cfg.xml

java - 使用 StoredProcedureQuery 时 session /EntityManager 关闭

java - 将 Heroku Spring Boot 模板降级到 Java 1.7

java - @Autowired HttpServletResponse

java: Spring Boot - Swagger2: 找不到 HTTP 请求的映射

java - 我的应用程序正在显示图像网址。如何下载图像以便它可以显示在我的应用程序上

java - 导入 com.amazonaws.services.dynamodbv2.document.DynamoDB;无法解析导入的文档部分

java - 使用 Hudson 覆盖 cvs 的本地模块目录

java - 通过后台删除hybris中的动态属性