Search in sources :

Example 51 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project nzbhydra2 by theotherp.

the class TvMazeHandler method searchByTitle.

private List<TvmazeShowSearch> searchByTitle(String title) throws InfoProviderException {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://api.tvmaze.com/search/shows").queryParam("q", title);
    ParameterizedTypeReference<List<TvmazeShowSearch>> typeRef = new ParameterizedTypeReference<List<TvmazeShowSearch>>() {
    };
    ResponseEntity<List<TvmazeShowSearch>> lookupResponse = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, null, typeRef);
    if (!lookupResponse.getStatusCode().is2xxSuccessful()) {
        throw new InfoProviderException("TVMaze lookup returned wrong status: " + lookupResponse.getStatusCode());
    }
    List<TvmazeShowSearch> shows = lookupResponse.getBody();
    if (shows.isEmpty()) {
        throw new InfoProviderException("TVMaze found no series with title " + title);
    }
    return shows;
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) List(java.util.List)

Example 52 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class JsonbHttpMessageConverterTests method readAndWriteParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertThat(results.size()).isEqualTo(1);
    MyBean result = results.get(0);
    assertThat(result.getString()).isEqualTo("Foo");
    assertThat(result.getNumber()).isEqualTo(42);
    assertThat(result.getFraction()).isCloseTo(42F, within(0F));
    assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
    assertThat(result.isBool()).isTrue();
    assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 53 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class MappingJackson2HttpMessageConverterTests method writeParameterizedBaseType.

@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<>() {
    };
    String body = "[{" + "\"bytes\":\"AQI=\"," + "\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42," + "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertThat(results.size()).isEqualTo(1);
    MyBean result = results.get(0);
    assertThat(result.getString()).isEqualTo("Foo");
    assertThat(result.getNumber()).isEqualTo(42);
    assertThat(result.getFraction()).isCloseTo(42F, within(0F));
    assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
    assertThat(result.isBool()).isTrue();
    assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(results, baseList.getType(), MediaType.APPLICATION_JSON, outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 54 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class MappingJackson2HttpMessageConverterTests method readAndWriteParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    String body = "[{" + "\"bytes\":\"AQI=\"," + "\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42," + "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertThat(results.size()).isEqualTo(1);
    MyBean result = results.get(0);
    assertThat(result.getString()).isEqualTo("Foo");
    assertThat(result.getNumber()).isEqualTo(42);
    assertThat(result.getFraction()).isCloseTo(42F, within(0F));
    assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
    assertThat(result.isBool()).isTrue();
    assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(results, beansList.getType(), MediaType.APPLICATION_JSON, outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 55 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class DefaultClientResponseTests method toEntityTypeReference.

@Test
public void toEntityTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<String> result = defaultClientResponse.toEntity(new ParameterizedTypeReference<String>() {
    }).block();
    assertThat(result.getBody()).isEqualTo("foo");
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)62 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)28 List (java.util.List)22 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 HttpEntity (org.springframework.http.HttpEntity)10 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)8 URISyntaxException (java.net.URISyntaxException)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 HttpMethod (org.springframework.http.HttpMethod)4