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 33cb4f90e0
commit 12bb231be4
17 changed files with 26288 additions and 0 deletions

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