【问题标题】:Java Spring Boot: How to response send redirect html to UIJava Spring Boot:如何响应将重定向 html 发送到 UI
【发布时间】:2016-12-30 04:24:48
【问题描述】:

我已经完成了示例 Spring Boot 应用程序。我不知道我错过了什么。

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>

我的应用程序类

@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");
  }
}

应用程序属性文件

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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@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

【问题讨论】:

  • 请发布您用于访问应用程序的网址
  • 检查您是否在 xml 中进行了正确的 url 映射
  • 首先检查是否可以使用localhost:8787/demo/help.html从浏览器直接访问help.html。如果不是,要么你的 Maven 目录有问题,要么如果 linux 有一些权限问题(前提是你从某个地方复制了文件)。其次,检查 application.properties 中 server.context-path 中 /demo 后是否没有空格。
  • @KarthikPrasad,感谢您的重播,浏览器正在使用 localhost:8787/demo/rest/test/,得到 404,并且仅在 win-7 中运行,请检查我更新的LOG

标签: java spring rest spring-mvc spring-boot


【解决方案1】:

首先我不明白你为什么使用@ResponseBody 一个返回类型为void 的方法。去掉它。然后你需要使用 View Resolver 来显示 html 页面,如果你想重定向它,你可以使用如下方法:

@Controller
public class RestControllerClass {  
@RequestMapping("/rest/test/")
  public String testOne(HttpServletResponse response) throws IOException{
    return "redirect:/help";
  }
}

HelpController.java

@Controller
public class HelpController {  
@RequestMapping("/help")
  public String get(HttpServletResponse response) throws IOException{
       return "help";
  }
}

在 src/main/resources 文件夹下的 application.properties 中。

spring.mvc.view.prefix=/static/
spring.mvc.view.suffix=.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2014-12-22
  • 2013-06-21
  • 1970-01-01
  • 2020-01-11
相关资源
最近更新 更多