【问题标题】:spring boot application not starting on configured port number in Tomcatspring boot 应用程序未在 Tomcat 中配置的端口号上启动
【发布时间】:2018-12-30 01:25:18
【问题描述】:

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

application.properties

hello.greeting=很高兴见到你

server.port=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/

【问题讨论】:

  • 没有看到代码我该如何解决您的问题?请提供导致问题的代码
  • 您仅指定了“/hello”上下文路径,因此您需要请求它localhost:9874/hello 而不仅仅是localhost:9874
  • 我也试过了,仍然出现 404 错误 localhost:9874/hello 服务器正在运行,没有任何错误
  • 我在想,我是否错过了任何其他配置。

标签: java spring spring-boot


【解决方案1】:

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

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

【讨论】:

  • 在 netstat -an 命令 [ TCP [::]:9874 [::]:0 LISTENING] 上,我看到这样,我的移植是否被任何其他服务使用?
  • 我已经关注了这篇文章,但我无法弄清楚问题所在。 spring.io/blog/2015/03/18/…
猜你喜欢
  • 2017-07-17
  • 2022-10-20
  • 1970-01-01
  • 2017-01-02
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
相关资源
最近更新 更多