JAVA-84: Move articles out of spring-boot part 1 (#9950)
* JAVA-84: Moved 1 article to spring-boot-actuator * JAVA-84: Moved 2 articles to spring-boot-mvc * JAVA-84: README changes for spring-boot module * JAVA-84: Moved classes to com.baeldung pkg to correct compilation issue
This commit is contained in:
@@ -9,4 +9,6 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
||||
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
|
||||
- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
- [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity)
|
||||
- [The @ServletComponentScan Annotation in Spring Boot](https://www.baeldung.com/spring-servletcomponentscan)
|
||||
- [Guide to Internationalization in Spring Boot](https://www.baeldung.com/spring-boot-internationalization)
|
||||
- More articles: [[next -->]](/spring-boot-modules/spring-boot-mvc-2)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
|
||||
/**
|
||||
* using the following annotations are equivalent:
|
||||
* <ul><li>
|
||||
* <code>@ServletComponentScan</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class})</code>
|
||||
* </li></ul>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.components")
|
||||
public class SpringBootAnnotatedApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootAnnotatedApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")
|
||||
public class SpringBootPlainApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class AttrListener implements ServletContextListener {
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
servletContextEvent.getServletContext().setAttribute("servlet-context-attr", "test");
|
||||
System.out.println("context init");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
||||
System.out.println("context destroy");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@WebServlet(name = "echo servlet", urlPatterns = "/echo")
|
||||
public class EchoServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
Path path = File.createTempFile("echo", "tmp").toPath();
|
||||
Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(path, response.getOutputStream());
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebFilter(urlPatterns = "/hello", description = "a filter for hello servlet", initParams = { @WebInitParam(name = "msg", value = "filtering ") }, filterName = "hello filter", servletNames = { "echo servlet" })
|
||||
public class HelloFilter implements Filter {
|
||||
|
||||
private FilterConfig filterConfig;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
System.out.println("filter init");
|
||||
this.filterConfig = filterConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
servletResponse.getOutputStream().print(filterConfig.getInitParameter("msg"));
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
System.out.println("filter destroy");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello") })
|
||||
public class HelloServlet extends HttpServlet {
|
||||
|
||||
private ServletConfig servletConfig;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig servletConfig) {
|
||||
this.servletConfig = servletConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
response.getOutputStream().write(servletConfig.getInitParameter("msg").getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.internationalization;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class InternationalizationApp {
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(InternationalizationApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.internationalization.config;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.internationalization.config")
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.US);
|
||||
return slr;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||
lci.setParamName("lang");
|
||||
return lci;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.internationalization.config;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class PageController {
|
||||
|
||||
@GetMapping("/international")
|
||||
public String getInternationalPage() {
|
||||
return "thymeleaf/international";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
email.notempty=Please provide valid email id.
|
||||
email.notempty=Please provide valid email id.
|
||||
greeting=Hello! Welcome to our website!
|
||||
lang.change=Change the language
|
||||
lang.eng=English
|
||||
lang.fr=French
|
||||
@@ -1 +1,5 @@
|
||||
email.notempty=Veuillez fournir un identifiant de messagerie valide.
|
||||
email.notempty=Veuillez fournir un identifiant de messagerie valide.
|
||||
greeting=Bonjour! Bienvenue sur notre site!
|
||||
lang.change=Changez la langue
|
||||
lang.eng=Anglais
|
||||
lang.fr=Francais
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
$(document).ready(function() {
|
||||
$("#locales").change(function () {
|
||||
var selectedOption = $('#locales').val();
|
||||
if (selectedOption != ''){
|
||||
window.location.replace('international?lang=' + selectedOption);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="ISO-8859-1" />
|
||||
<title>Home</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="internationalization.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 th:text="#{greeting}"></h1>
|
||||
|
||||
<br /><br />
|
||||
<span th:text="#{lang.change}"></span>:
|
||||
<select id="locales">
|
||||
<option value=""></option>
|
||||
<option value="en" th:text="#{lang.eng}"></option>
|
||||
<option value="fr" th:text="#{lang.fr}"></option>
|
||||
</select>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
|
||||
public class SpringBootWithServletComponentIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
|
||||
assertNotNull(servletContext);
|
||||
assertNotNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
assertEquals("test", servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNotNull(filterRegistration);
|
||||
assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet"));
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletFilter_whenGetHello_thenRequestFiltered() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/hello", String.class);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertEquals("filtering hello", responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.postForEntity("/echo", "echo", String.class);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertEquals("filtering echo", responseEntity.getBody());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
|
||||
public class SpringBootWithoutServletComponentIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenNotFound() {
|
||||
assertNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletFilter_whenGetHello_thenEndpointNotFound() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/hello", String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckFilterMappings_thenEmpty() {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNull(filterRegistration);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user