Add Jsonb support

Spring Framework 5 will support Jsonb as a HttpMessageConverter, this
commit adds auto-configuration support. Also, support for Jsonb in the
@JsonTest has been added.

This implementation is running against Apache Johnzon

See gh-9648
This commit is contained in:
Eddú Meléndez
2017-04-10 21:01:13 -05:00
committed by Stephane Nicoll
parent 676dec8cf7
commit 97aeaa4a32
22 changed files with 674 additions and 16 deletions

View File

@@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.http;
import java.util.Arrays;
import java.util.List;
import javax.json.bind.Jsonb;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.junit.After;
@@ -28,6 +30,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -38,6 +41,7 @@ import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessage
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
@@ -51,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author David Liu
* @author Andy Wilkinson
* @author Sebastien Deleuze
* @author Eddú Meléndez
*/
public class HttpMessageConvertersAutoConfigurationTests {
@@ -140,19 +145,20 @@ public class HttpMessageConvertersAutoConfigurationTests {
@Test
public void jacksonIsPreferredByDefaultWhenBothGsonAndJacksonAreAvailable() {
this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
assertConverterBeanExists(MappingJackson2HttpMessageConverter.class,
"mappingJackson2HttpMessageConverter");
assertConverterBeanRegisteredWithHttpMessageConverters(
MappingJackson2HttpMessageConverter.class);
assertThat(this.context.getBeansOfType(GsonHttpMessageConverter.class)).isEmpty();
assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class)).isEmpty();
}
@Test
public void gsonCanBePreferredWhenBothGsonAndJacksonAreAvailable() {
this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class);
TestPropertyValues.of("spring.http.converters.preferred-json-mapper:gson")
.applyTo(this.context);
this.context.refresh();
@@ -160,6 +166,8 @@ public class HttpMessageConvertersAutoConfigurationTests {
"gsonHttpMessageConverter");
assertConverterBeanRegisteredWithHttpMessageConverters(
GsonHttpMessageConverter.class);
assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class))
.isEmpty();
assertThat(this.context.getBeansOfType(MappingJackson2HttpMessageConverter.class))
.isEmpty();
}
@@ -176,6 +184,55 @@ public class HttpMessageConvertersAutoConfigurationTests {
GsonHttpMessageConverter.class);
}
@Test
public void noJsonb() throws Exception {
this.context.register(HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(Jsonb.class).isEmpty()).isTrue();
assertThat(this.context.getBeansOfType(JsonbHttpMessageConverter.class).isEmpty())
.isTrue();
}
@Test
public void defaultJsonbConverter() throws Exception {
this.context.register(JsonbAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
assertConverterBeanExists(JsonbHttpMessageConverter.class,
"jsonbHttpMessageConverter");
assertConverterBeanRegisteredWithHttpMessageConverters(
JsonbHttpMessageConverter.class);
}
@Test
public void jsonbCanBePreferredWhenBothGsonAndJacksonAreAvailable() {
this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class,
JsonbAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class);
TestPropertyValues.of("spring.http.converters.preferred-json-mapper:jsonb")
.applyTo(this.context);
this.context.refresh();
assertConverterBeanExists(JsonbHttpMessageConverter.class,
"jsonbHttpMessageConverter");
assertConverterBeanRegisteredWithHttpMessageConverters(
JsonbHttpMessageConverter.class);
assertThat(this.context.getBeansOfType(GsonHttpMessageConverter.class))
.isEmpty();
assertThat(this.context.getBeansOfType(MappingJackson2HttpMessageConverter.class))
.isEmpty();
}
@Test
public void customJsonbConverter() throws Exception {
this.context.register(JsonbAutoConfiguration.class, JsonbConverterConfig.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
assertConverterBeanExists(JsonbHttpMessageConverter.class,
"customJsonbMessageConverter");
assertConverterBeanRegisteredWithHttpMessageConverters(
JsonbHttpMessageConverter.class);
}
@Test
public void defaultStringConverter() throws Exception {
this.context.register(HttpMessageConvertersAutoConfiguration.class);
@@ -288,6 +345,18 @@ public class HttpMessageConvertersAutoConfigurationTests {
}
@Configuration
protected static class JsonbConverterConfig {
@Bean
public JsonbHttpMessageConverter customJsonbMessageConverter(Jsonb jsonb) {
JsonbHttpMessageConverter converter = new JsonbHttpMessageConverter();
converter.setJsonb(jsonb);
return converter;
}
}
@Configuration
protected static class StringConverterConfig {

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jsonb;
import javax.json.bind.Jsonb;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JsonbAutoConfiguration}.
*
* @author Eddú Meléndez
*/
public class JsonbAutoConfigurationTests {
AnnotationConfigApplicationContext context;
@Before
public void setUp() {
this.context = new AnnotationConfigApplicationContext();
}
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void jsonbRegistration() {
this.context.register(JsonbAutoConfiguration.class);
this.context.refresh();
Jsonb jsonb = this.context.getBean(Jsonb.class);
assertThat(jsonb.toJson(new DataObject())).isEqualTo("{\"data\":\"hello\"}");
}
public class DataObject {
@SuppressWarnings("unused")
private String data = "hello";
public String getData() {
return this.data;
}
public void setData(String data) {
this.data = data;
}
}
}