Search in sources :

Example 1 with JSON_VIEW_HINT

use of org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT in project spring-framework by spring-projects.

the class BodyExtractorsTests method toFluxWithHints.

@Test
public void toFluxWithHints() {
    BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    String text = "[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]";
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Flux<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("foo");
        assertThat(user.getPassword()).isNull();
    }).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("bar");
        assertThat(user.getPassword()).isNull();
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) FilePart(org.springframework.http.codec.multipart.FilePart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) JsonView(com.fasterxml.jackson.annotation.JsonView) StepVerifier(reactor.test.StepVerifier) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) HashMap(java.util.HashMap) JSON_VIEW_HINT(org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) Optional(java.util.Optional) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with JSON_VIEW_HINT

use of org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT in project spring-framework by spring-projects.

the class Jackson2JsonDecoderTests method fieldLevelJsonView.

@Test
public void fieldLevelJsonView() {
    Flux<DataBuffer> input = Flux.from(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
    ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
    Map<String, Object> hints = Collections.singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
    testDecode(input, elementType, step -> step.consumeNextWith(value -> {
        JacksonViewBean bean = (JacksonViewBean) value;
        assertThat(bean.getWithView1()).isEqualTo("with");
        assertThat(bean.getWithView2()).isNull();
        assertThat(bean.getWithoutView()).isNull();
    }), null, hints);
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DecodingException(org.springframework.core.codec.DecodingException) JSON_VIEW_HINT(org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) CodecException(org.springframework.core.codec.CodecException) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) MimeType(org.springframework.util.MimeType) APPLICATION_NDJSON(org.springframework.http.MediaType.APPLICATION_NDJSON) BigDecimal(java.math.BigDecimal) MyJacksonView1(org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1) Charset(java.nio.charset.Charset) Map(java.util.Map) MyJacksonView3(org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResolvableType(org.springframework.core.ResolvableType) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pojo(org.springframework.web.testfixture.xml.Pojo) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) APPLICATION_STREAM_JSON(org.springframework.http.MediaType.APPLICATION_STREAM_JSON) Flux(reactor.core.publisher.Flux) List(java.util.List) AbstractDecoderTests(org.springframework.core.testfixture.codec.AbstractDecoderTests) Collections(java.util.Collections) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

StandardCharsets (java.nio.charset.StandardCharsets)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 MediaType (org.springframework.http.MediaType)2 JSON_VIEW_HINT (org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 StepVerifier (reactor.test.StepVerifier)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1