[英]Spring Boot Getting Startet 404 error
I'm trying to use the spring framework for the first time.我第一次尝试使用spring框架。 Unfortunately I'm getting 404-error when I call the URL of my webservice.
不幸的是,当我调用我的网络服务的 URL 时,我收到了 404 错误。 Tomcat deployment of the war file is working (no errors in the logs).
war 文件的 Tomcat 部署正在运行(日志中没有错误)。 When I execute the Example.java in eclipse everythings works fine.
当我在 Eclipse 中执行 Example.java 时,一切正常。
Where is the mistake?错误在哪里?
See my files/logs:查看我的文件/日志:
Example.java Example.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
build.gradle构建.gradle
buildscript {
repositories {
jcenter()
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'http://repo.spring.io/milestone' }
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'myproject'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
//compile 'org.slf4j:slf4j-api:1.7.21'
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
//testCompile 'junit:junit:4.12'
}
log when starting the application inside eclipse在eclipse中启动应用程序时记录
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.BUILD-SNAPSHOT)
2017-06-01 16:22:17.745 INFO 14940 --- [ main] Example : Starting Example on Laptop-Timo with PID 14940 (started by Timo in C:\Users\Timo\Documents\SE\Semester 4\LabSW\Webservice\TimeShoppingService)
2017-06-01 16:22:17.745 INFO 14940 --- [ main] Example : No active profile set, falling back to default profiles: default
2017-06-01 16:22:17.815 INFO 14940 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6a01e23: startup date [Thu Jun 01 16:22:17 CEST 2017]; root of context hierarchy
2017-06-01 16:22:20.098 INFO 14940 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2017-06-01 16:22:20.116 INFO 14940 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-06-01 16:22:20.116 INFO 14940 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-01 16:22:20.264 INFO 14940 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-01 16:22:20.264 INFO 14940 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2449 ms
2017-06-01 16:22:20.626 INFO 14940 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-01 16:22:20.638 INFO 14940 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-01 16:22:20.639 INFO 14940 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-01 16:22:20.639 INFO 14940 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-01 16:22:20.640 INFO 14940 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-01 16:22:21.343 INFO 14940 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6a01e23: startup date [Thu Jun 01 16:22:17 CEST 2017]; root of context hierarchy
2017-06-01 16:22:21.442 INFO 14940 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String Example.home()
2017-06-01 16:22:21.456 INFO 14940 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-01 16:22:21.457 INFO 14940 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-01 16:22:21.548 INFO 14940 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-01 16:22:21.549 INFO 14940 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-01 16:22:21.683 INFO 14940 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-01 16:22:21.940 INFO 14940 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-06-01 16:22:22.018 INFO 14940 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http)
2017-06-01 16:22:22.024 INFO 14940 --- [ main] Example : Started Example in 4.81 seconds (JVM running for 5.487)
2017-06-01 16:22:32.727 INFO 14940 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-06-01 16:22:32.727 INFO 14940 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-06-01 16:22:32.755 INFO 14940 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 28 ms
Use @RestController
instead of @Controller
and just give request like使用
@RestController
而不是@Controller
并给出类似的请求
In order to deploy a spring-boot application to an external servlet container you need to extend SpringBootServletInitializer
.为了将 spring-boot 应用程序部署到外部 servlet 容器,您需要扩展
SpringBootServletInitializer
。
The main method is only executed when the application is run standalone. main 方法仅在应用程序独立运行时执行。
It is recommended to run spring-boot apps standalone rather than via an external servlet container, but it is possible to support both methods simultaneously.建议独立运行 spring-boot 应用程序,而不是通过外部 servlet 容器,但可以同时支持这两种方法。
See the spring documentation for more information. 有关更多信息,请参阅弹簧文档。
By Default context path of Spring boot application is '/'.默认情况下,Spring 引导应用程序的上下文路径是“/”。 So in your case url to access home resource will be http://localhost:8080/
因此,在您的情况下,访问主页资源的 url 将是http://localhost:8080/
For custom context path add an application.properties file to src\main\resources.
对于自定义上下文路径,将 application.properties 文件添加到 src\main\resources。 In that properties file, add 2 properties:
在该属性文件中,添加 2 个属性:
server.contextPath=/Service server.port=12378
Maybe url mistake.可能是网址错误。 try it like the url below .
试试下面的网址。
http://localhost:8080/Service/
You have not mapped your controller or method with any specific string.您尚未使用任何特定字符串映射您的控制器或方法。
@Controller
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
...
You have used @RequestMapping("/")
which maps the home()
method so you need to use localhost:8080/
url instead of localhost:8080/service
and if you want to use the service
in url just change mapping to @RequestMapping("/service")
.您已经使用了映射
home()
方法的@RequestMapping("/")
,因此您需要使用localhost:8080/
url 而不是localhost:8080/service
如果您想在 url 中使用该service
,只需将映射更改为@RequestMapping("/service")
。
The easiest way out is just by replacing the annotation above the class respectively.最简单的方法就是分别替换类上方的注释。 And use the
@RestController
, then you move the @RequestMapping("/")
above the class, Beneath the rest controller.并使用
@RestController
,然后将@RequestMapping("/")
移动到类上方,其余控制器下方。 Go to the Postman and call it with localhost:8080/
and that's all转到邮递员并使用
localhost:8080/
调用它,仅此而已
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.