Skip to content

Commit

Permalink
Add WelcomePageServlet
Browse files Browse the repository at this point in the history
see #101
  • Loading branch information
larsgrefer committed Oct 2, 2024
1 parent 33cc80d commit f0ac4db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
* @author Lars Grefer
*/
@SpringBootApplication
@ServletComponentScan
public class JarExampleApplication {

public static void main(String[] args) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/joinfaces/example/WelcomePageServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.joinfaces.example;

import jakarta.servlet.GenericServlet;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* @author Lars Grefer
*/
@WebServlet(urlPatterns = "/")
public class WelcomePageServlet extends GenericServlet {

@Override
public void service(ServletRequest req, ServletResponse res) throws IOException {
if (res instanceof HttpServletResponse httpServletResponse) {
httpServletResponse.sendRedirect("/index.xhtml");
}
}
}
11 changes: 11 additions & 0 deletions src/test/java/org/joinfaces/example/JarExampleApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public class JarExampleApplicationTest {
public void testHelloFromSpring() {
ResponseEntity<String> entity = restTemplate.getForEntity("/index.xhtml", String.class);

checkResponse(entity);
}

@Test
public void testWelcomePageRedirect() {
ResponseEntity<String> entity = restTemplate.getForEntity("/", String.class);

checkResponse(entity);
}

private static void checkResponse(ResponseEntity<String> entity) {
assertThat(entity.getStatusCode().is2xxSuccessful()).isTrue();

assertThat(entity.getBody()).contains("Hello from Spring:");
Expand Down

0 comments on commit f0ac4db

Please sign in to comment.