JAVA-8286 Split or move spring boot runtime module

This commit is contained in:
mikr
2021-11-14 11:08:17 +01:00
parent 7295717c59
commit 9798b29143
11 changed files with 6 additions and 2 deletions

View File

@@ -8,7 +8,5 @@ This module contains articles about administering a Spring Boot runtime
- [Logging HTTP Requests with Spring Boot Actuator HTTP Tracing](https://www.baeldung.com/spring-boot-actuator-http)
- [Spring Boot Embedded Tomcat Logs](https://www.baeldung.com/spring-boot-embedded-tomcat-logs)
- [Project Configuration with Spring](https://www.baeldung.com/project-configuration-with-spring)
- [CORS with Spring](https://www.baeldung.com/spring-cors)
- [Spring Log Incoming Requests](https://www.baeldung.com/spring-http-logging)
- [How to Configure Spring Boot Tomcat](https://www.baeldung.com/spring-boot-configure-tomcat)
- [Max-HTTP-Header-Size in Spring Boot 2](https://www.baeldung.com/spring-boot-max-http-header-size)

View File

@@ -1,18 +0,0 @@
package com.baeldung.cors;
public class Account {
private Long id;
public Account(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@@ -1,24 +0,0 @@
package com.baeldung.cors;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
@CrossOrigin("http://example.com")
@RequestMapping(method = RequestMethod.GET, path = "/{id}")
public Account retrieve(@PathVariable Long id) {
return new Account(id);
}
@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
public void remove(@PathVariable Long id) {
// ...
}
}

View File

@@ -1,16 +0,0 @@
package com.baeldung.cors.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}

View File

@@ -1,15 +0,0 @@
package com.baeldung.maxhttpheadersize;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan("com.baeldung.maxhttpheadersize")
public class MaxHttpHeaderSizeApplication {
public static void main(final String[] args) {
SpringApplication.run(MaxHttpHeaderSizeApplication.class, args);
}
}

View File

@@ -1,17 +0,0 @@
package com.baeldung.maxhttpheadersize.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan({ "com.baeldung.maxhttpheadersize.*" })
public class MaxHTTPHeaderSizeConfig implements WebMvcConfigurer {
public MaxHTTPHeaderSizeConfig() {
super();
}
}

View File

@@ -1,17 +0,0 @@
package com.baeldung.maxhttpheadersize.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/request-header-test")
public class MaxHttpHeaderSizeController {
@GetMapping
public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) {
return true;
}
}

View File

@@ -1,52 +0,0 @@
package com.baeldung.maxhttpheadersize.controller;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.maxhttpheadersize.config.MaxHTTPHeaderSizeConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MaxHTTPHeaderSizeConfig.class)
@WebAppConfiguration
public class MaxHttpHeaderSizeControllerIntegrationTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception {
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
.with(httpBasic("user", "password"))
.header("token", "token"))
.andExpect(status().isOk());
}
@Test
public void givenTokenIsMissingInHeader_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
.with(httpBasic("user", "password")))
.andExpect(status().isBadRequest());
}
}

View File

@@ -1,48 +0,0 @@
package com.baeldung.maxhttpheadersize.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
// Start MaxHttpHeaderSizeController Spring Boot App(MaxHttpHeaderSizeApplication) first
public class MaxHttpHeaderSizeControllerLiveTest {
@Test(expected = HttpClientErrorException.class)
public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
final String url = "http://localhost:8080/request-header-test";
HttpHeaders headers = new HttpHeaders();
headers.set("token", readRandomStringFromFile());
HttpEntity entity = new HttpEntity(headers);
final ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class);
}
static String readRandomStringFromFile() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();
String content = stringBuilder.toString();
return content;
}
}