Kixeye User
Kixeye User

Reputation: 41

Java Spring Boot: How to response send redirect html to UI

I have done sample spring boot app. I do not know what I am missing.

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

My help.html file under resource & static folder.

I am getting 404 error.

**UPDATE - 1 ** - this is my LOG.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

Upvotes: 1

Views: 8645

Answers (1)

erhun
erhun

Reputation: 3649

First of all i don't understand why you use @ResponseBody a method which return type void. Remove it. Then you need to use View Resolver to show html page and if you want to redirect it you can use an approach like below:

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

in application.properties under src/main/resources folder.

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

Upvotes: 3

Related Questions