data buffer limit exception (#12862)

* data buffer limit exception

* removed mvn scripts

* test cases

* updated integration test name

Co-authored-by: s9m33r <no-reply>
This commit is contained in:
Sameer 2022-10-16 09:07:24 +05:30 committed by GitHub
parent a0aa008362
commit 2c33928f44
17 changed files with 26288 additions and 0 deletions

View File

@ -25,6 +25,7 @@
<module>spring-5-reactive-oauth</module>
<module>spring-5-reactive-security</module>
<module>spring-reactive</module>
<module>spring-reactive-exceptions</module>
</modules>
<build>

View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@ -0,0 +1,55 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring.reactive</groupId>
<artifactId>spring-reactive-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>spring-reactive-exceptions</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-reactive-exceptions</name>
<description>A module to hold demo examples related to exception in Spring Reactive</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.reactive.springreactiveexceptions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringReactiveExceptionsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringReactiveExceptionsApplication.class, args);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.spring.reactive.springreactiveexceptions.client;
import com.baeldung.spring.reactive.springreactiveexceptions.model.Users;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
@Service
public class DemoSelfClient {
@Autowired
@Qualifier("webClient")
private WebClient webClient;
@Autowired
private ObjectMapper objectMapper;
public Mono<Users> fetch() {
return webClient
.post()
.uri("/1.0/process")
.body(BodyInserters.fromPublisher(readRequestBody(), Users.class))
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(Users.class));
}
private Mono<Users> readRequestBody() {
return Mono
.fromCallable(() -> objectMapper.readValue(new ClassPathResource("390KB.json")
.getURL(), Users.class))
.subscribeOn(Schedulers.boundedElastic());
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.spring.reactive.springreactiveexceptions.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class BeanConfiguration {
@Value("${host:http://localhost:8080}")
private String host;
@Bean("webClient")
public WebClient getSelfWebClient(WebClient.Builder builder) {
return builder
.baseUrl(host)
.build();
}
@Bean("progWebClient")
public WebClient getProgSelfWebClient() {
return WebClient
.builder()
.baseUrl(host)
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(codecs -> codecs
.defaultCodecs()
.maxInMemorySize(500 * 1024))
.build())
.build();
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.spring.reactive.springreactiveexceptions.config;
import com.baeldung.spring.reactive.springreactiveexceptions.handler.DataProcessingHandler;
import com.baeldung.spring.reactive.springreactiveexceptions.handler.TriggerHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class Router {
@Autowired
private DataProcessingHandler dataProcessingHandler;
@Autowired
private TriggerHandler triggerHandler;
@Bean
public RouterFunction<ServerResponse> getRoutes() {
return RouterFunctions
.route()
.POST("/1.0/process", dataProcessingHandler)
.POST("/1.0/trigger", triggerHandler)
.build();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.reactive.springreactiveexceptions.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@Configuration
public class WebFluxConfiguration implements WebFluxConfigurer {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.defaultCodecs().maxInMemorySize(500 * 1024);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.spring.reactive.springreactiveexceptions.handler;
import com.baeldung.spring.reactive.springreactiveexceptions.model.Users;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class DataProcessingHandler implements HandlerFunction<ServerResponse> {
@Override
public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse
.ok()
.body(request
.bodyToMono(Users.class), Users.class);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.spring.reactive.springreactiveexceptions.handler;
import com.baeldung.spring.reactive.springreactiveexceptions.client.DemoSelfClient;
import com.baeldung.spring.reactive.springreactiveexceptions.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class TriggerHandler implements HandlerFunction<ServerResponse> {
@Autowired
private DemoSelfClient demoSelfClient;
@Override
public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse
.ok()
.body(demoSelfClient.fetch(), Users.class);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.spring.reactive.springreactiveexceptions.model;
import lombok.Data;
@Data
public class Friend {
private Integer id;
private String name;
}

View File

@ -0,0 +1,31 @@
package com.baeldung.spring.reactive.springreactiveexceptions.model;
import lombok.Data;
import java.util.List;
@Data
public class User {
private String _id;
private String index;
private String guid;
private Boolean isActive;
private String balance;
private String picture;
private Integer age;
private String eyeColor;
private String name;
private String gender;
private String company;
private String email;
private String phone;
private String address;
private String about;
private String registered;
private Integer latitude;
private Integer longitude;
private List<String> tags;
private List<Friend> friends;
private String greeting;
private String favouriteFruit;
}

View File

@ -0,0 +1,10 @@
package com.baeldung.spring.reactive.springreactiveexceptions.model;
import lombok.Data;
import java.util.List;
@Data
public class Users {
private List<User> users;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
spring:
codec:
max-in-memory-size: 500KB

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.reactive.springreactiveexceptions;
import com.baeldung.spring.reactive.springreactiveexceptions.model.Users;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import java.io.IOException;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SpringReactiveExceptionsApplicationIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Autowired
private ObjectMapper objectMapper;
@Test
void givenARequestBody_whenProcessIsCalled_thenReturnTheBodyAsIs() throws IOException {
Users users = objectMapper.readValue(new ClassPathResource("390KB.json")
.getURL(), Users.class);
webTestClient
.post()
.uri("1.0/process")
.body(BodyInserters.fromValue(users))
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.OK)
.expectBody(Users.class)
.isEqualTo(users);
}
@Test
void whenTriggerIsCalled_thenReturnTheExpectedJSONResponse() throws IOException {
Users users = objectMapper.readValue(new ClassPathResource("390KB.json")
.getURL(), Users.class);
webTestClient
.post()
.uri("1.0/trigger")
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.OK)
.expectBody(Users.class)
.isEqualTo(users);
}
}

File diff suppressed because it is too large Load Diff