我已經完成了示例spring引導應用程序。我不知道我錯過了什麼。Java Spring Boot:如何響應發送重定向html到UI
的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootRestEx</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
MyApplicationClass
@SpringBootApplication
public class SpringBootRestExApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestExApplication.class, args);
}
}
MyRestCallClass
@RestController
public class RestControllerClass {
@RequestMapping("/rest/test/")
public @ResponseBody void testOne(HttpServletResponse response) throws IOException{
response.sendRedirect("help.html");
}
}
AplicationProperyfile
server.port=8787
server.context-path=/demo
我下資源&靜態文件夾help.html文件。
我得到404錯誤。
**更新 - 1 ** - 這是我的日誌。
. ____ _ __ _ _
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ))))
' |____| .__|_| |_|_| |_\__, |////
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2016-12-30 12:49:58.781 INFO 9576 --- [ main] com.example.SpringBootRestExApplication : Starting SpringBootRestExApplication on Hydrogen with PID 9576 (D:\18062016_AceKab_WorkShop\10-11-2016_mysql_Acekab\SpringBootRestEx\target\classes started by ace in D:\18062016_AceKab_WorkShop\10-11-2016_mysql_Acekab\SpringBootRestEx)
2016-12-30 12:49:58.788 INFO 9576 --- [ main] com.example.SpringBootRestExApplication : No active profile set, falling back to default profiles: default
2016-12-30 12:49:58.940 INFO 9576 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]1622f1b: startup date [Fri Dec 30 12:49:58 IST 2016]; root of context hierarchy
2016-12-30 12:50:02.411 INFO 9576 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8787 (http)
2016-12-30 12:50:02.438 INFO 9576 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-12-30 12:50:02.440 INFO 9576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2016-12-30 12:50:02.718 INFO 9576 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring embedded WebApplicationContext
2016-12-30 12:50:02.719 INFO 9576 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3788 ms
2016-12-30 12:50:03.145 INFO 9576 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-12-30 12:50:03.154 INFO 9576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-12-30 12:50:03.155 INFO 9576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-12-30 12:50:03.156 INFO 9576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-12-30 12:50:03.156 INFO 9576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-12-30 12:50:03.965 INFO 9576 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]1622f1b: startup date [Fri Dec 30 12:49:58 IST 2016]; root of context hierarchy
2016-12-30 12:50:04.126 INFO 9576 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/rest/test/]}" onto public void com.example.RestControllerClass.testOne(javax.servlet.http.HttpServletResponse) throws java.io.IOException
2016-12-30 12:50:04.137 INFO 9576 --- [ 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.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-12-30 12:50:04.138 INFO 9576 --- [ 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.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-12-30 12:50:04.222 INFO 9576 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 12:50:04.222 INFO 9576 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 12:50:04.313 INFO 9576 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 12:50:04.628 INFO 9576 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-12-30 12:50:04.803 INFO 9576 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8787 (http)
2016-12-30 12:50:04.820 INFO 9576 --- [ main] com.example.SpringBootRestExApplication : Started SpringBootRestExApplication in 6.889 seconds (JVM running for 7.666)
2016-12-30 12:52:10.192 INFO 9576 --- [nio-8787-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-12-30 12:52:10.192 INFO 9576 --- [nio-8787-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-12-30 12:52:10.222 INFO 9576 --- [nio-8787-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 30 ms
請張貼烏爾用來訪問應用 – Valath
檢查是否已在XML –
首先檢查做了正確的URL映射如果你可以從瀏覽器中使用HTTP直接訪問help.html的網址://本地主機:8787 /demo/help.html。如果沒有,你有Maven目錄有一些問題,或者如果Linux的一些權限問題(只要你從某處複製文件)。其次,檢查application.properties中server.context-path中的/ demo後是否沒有空格。 –