Polish
This commit is contained in:
@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
@@ -56,8 +57,8 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Andy Wilkinson
|
||||
* @author Sebastien Deleuze
|
||||
* @author Marcel Overdijk
|
||||
* @author Sebastien Deleuze
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -99,29 +100,25 @@ public class JacksonAutoConfiguration {
|
||||
static class JacksonObjectMapperBuilderAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private HttpMapperProperties httpMapperProperties = new HttpMapperProperties();
|
||||
private JacksonProperties jacksonProperties;
|
||||
|
||||
@Autowired
|
||||
private JacksonProperties jacksonProperties = new JacksonProperties();
|
||||
private HttpMapperProperties httpMapperProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
||||
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
|
||||
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
||||
|
||||
if (this.httpMapperProperties.isJsonSortKeys()) {
|
||||
builder.featuresToEnable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
|
||||
}
|
||||
|
||||
configureFeatures(builder, this.jacksonProperties.getDeserialization());
|
||||
configureFeatures(builder, this.jacksonProperties.getSerialization());
|
||||
configureFeatures(builder, this.jacksonProperties.getMapper());
|
||||
configureFeatures(builder, this.jacksonProperties.getParser());
|
||||
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
||||
|
||||
configureDateFormat(builder);
|
||||
configurePropertyNamingStrategy(builder);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -137,56 +134,60 @@ public class JacksonAutoConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
private void configurePropertyNamingStrategy(Jackson2ObjectMapperBuilder builder) {
|
||||
// We support a fully qualified class name extending Jackson's
|
||||
// PropertyNamingStrategy or a string value corresponding to the constant
|
||||
// names in PropertyNamingStrategy which hold default provided implementations
|
||||
String propertyNamingStrategy = this.jacksonProperties
|
||||
.getPropertyNamingStrategy();
|
||||
if (propertyNamingStrategy != null) {
|
||||
try {
|
||||
Class<?> clazz = ClassUtils.forName(propertyNamingStrategy, null);
|
||||
builder.propertyNamingStrategy((PropertyNamingStrategy) BeanUtils
|
||||
.instantiateClass(clazz));
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
// Find the field (this way we automatically support new constants
|
||||
// that may be added by Jackson in the future)
|
||||
Field field = ReflectionUtils.findField(PropertyNamingStrategy.class,
|
||||
propertyNamingStrategy, PropertyNamingStrategy.class);
|
||||
if (field != null) {
|
||||
try {
|
||||
builder.propertyNamingStrategy((PropertyNamingStrategy) field
|
||||
.get(null));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Constant named '"
|
||||
+ propertyNamingStrategy + "' not found on "
|
||||
+ PropertyNamingStrategy.class.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {
|
||||
// We support a fully qualified class name extending DateFormat or a date
|
||||
// pattern string value
|
||||
String dateFormat = this.jacksonProperties.getDateFormat();
|
||||
if (dateFormat != null) {
|
||||
try {
|
||||
Class<?> clazz = ClassUtils.forName(dateFormat, null);
|
||||
builder.dateFormat((DateFormat) BeanUtils.instantiateClass(clazz));
|
||||
Class<?> dateFormatClass = ClassUtils.forName(dateFormat, null);
|
||||
builder.dateFormat((DateFormat) BeanUtils
|
||||
.instantiateClass(dateFormatClass));
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
catch (ClassNotFoundException ex) {
|
||||
builder.dateFormat(new SimpleDateFormat(dateFormat));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void configurePropertyNamingStrategy(Jackson2ObjectMapperBuilder builder) {
|
||||
// We support a fully qualified class name extending Jackson's
|
||||
// PropertyNamingStrategy or a string value corresponding to the constant
|
||||
// names in PropertyNamingStrategy which hold default provided implementations
|
||||
String strategy = this.jacksonProperties.getPropertyNamingStrategy();
|
||||
if (strategy != null) {
|
||||
try {
|
||||
configurePropertyNamingStrategyClass(builder,
|
||||
ClassUtils.forName(strategy, null));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
configurePropertyNamingStrategyField(builder, strategy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void configurePropertyNamingStrategyClass(
|
||||
Jackson2ObjectMapperBuilder builder, Class<?> propertyNamingStrategyClass) {
|
||||
builder.propertyNamingStrategy((PropertyNamingStrategy) BeanUtils
|
||||
.instantiateClass(propertyNamingStrategyClass));
|
||||
}
|
||||
|
||||
private void configurePropertyNamingStrategyField(
|
||||
Jackson2ObjectMapperBuilder builder, String fieldName) {
|
||||
// Find the field (this way we automatically support new constants
|
||||
// that may be added by Jackson in the future)
|
||||
Field field = ReflectionUtils.findField(PropertyNamingStrategy.class,
|
||||
fieldName, PropertyNamingStrategy.class);
|
||||
Assert.notNull(field, "Constant named '" + fieldName + "' not found on "
|
||||
+ PropertyNamingStrategy.class.getName());
|
||||
try {
|
||||
builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -29,6 +29,7 @@ import org.glassfish.jersey.servlet.ServletProperties;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -44,8 +45,9 @@ import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.filter.RequestContextFilter;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Jersey.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ SpringComponentProvider.class, ServletRegistration.class })
|
||||
@@ -65,7 +67,7 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
|
||||
|
||||
@PostConstruct
|
||||
public void path() {
|
||||
path = findPath(AnnotationUtils.findAnnotation(config.getClass(),
|
||||
this.path = findPath(AnnotationUtils.findAnnotation(this.config.getClass(),
|
||||
ApplicationPath.class));
|
||||
}
|
||||
|
||||
@@ -78,9 +80,9 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "jerseyServletRegistration")
|
||||
public ServletRegistrationBean jerseyServletRegistration() {
|
||||
Class<? extends ResourceConfig> configType = config.getClass();
|
||||
Class<? extends ResourceConfig> configType = this.config.getClass();
|
||||
ServletRegistrationBean registration = new ServletRegistrationBean(
|
||||
new ServletContainer(), path);
|
||||
new ServletContainer(), this.path);
|
||||
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,
|
||||
configType.getName());
|
||||
registration.setName("jerseyServlet");
|
||||
@@ -100,7 +102,7 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
|
||||
return "/*";
|
||||
}
|
||||
String path = annotation.value();
|
||||
return path.isEmpty() || path.equals("/") ? "/*" : path + "/*";
|
||||
return ((path.isEmpty() || path.equals("/")) ? "/*" : path + "/*");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
* JTA Configuration for a JNDI-managed {@link JtaTransactionManager}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -41,8 +42,7 @@ class JndiJtaConfiguration {
|
||||
|
||||
@Bean
|
||||
public JtaTransactionManager transactionManager() {
|
||||
JtaTransactionManagerFactoryBean factoryBean = new JtaTransactionManagerFactoryBean();
|
||||
return factoryBean.getObject();
|
||||
return new JtaTransactionManagerFactoryBean().getObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ import java.util.Map;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -37,6 +35,8 @@ import org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
|
||||
@@ -50,6 +50,7 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Josh Long
|
||||
* @author Manuel Doninger
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
|
||||
@@ -60,8 +61,12 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
|
||||
|
||||
private static final String JTA_PLATFORM = "hibernate.transaction.jta.platform";
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(HibernateJpaAutoConfiguration.class);
|
||||
/**
|
||||
* {@code NoJtaPlatform} implementations for various Hibernate versions.
|
||||
*/
|
||||
private static final String NO_JTA_PLATFORM_CLASSES[] = {
|
||||
"org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform",
|
||||
"org.hibernate.service.jta.platform.internal.NoJtaPlatform" };
|
||||
|
||||
@Autowired
|
||||
private JpaProperties properties;
|
||||
@@ -84,38 +89,35 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
|
||||
@Override
|
||||
protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
|
||||
super.customizeVendorProperties(vendorProperties);
|
||||
|
||||
String HIBERNATE43_NOJTAPLATFORM_CLASS = "org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform";
|
||||
String HIBERNATE42_NOJTAPLATFORM_CLASS = "org.hibernate.service.jta.platform.internal.NoJtaPlatform";
|
||||
|
||||
if (!vendorProperties.containsKey(JTA_PLATFORM)) {
|
||||
JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
|
||||
try {
|
||||
if (jtaTransactionManager != null) {
|
||||
vendorProperties.put(JTA_PLATFORM, new SpringJtaPlatform(
|
||||
jtaTransactionManager));
|
||||
}
|
||||
else {
|
||||
Object jtaPlatform = null;
|
||||
if (ClassUtils.isPresent(HIBERNATE43_NOJTAPLATFORM_CLASS, null)) {
|
||||
jtaPlatform = ClassUtils.forName(HIBERNATE43_NOJTAPLATFORM_CLASS,
|
||||
null).newInstance();
|
||||
}
|
||||
else if (ClassUtils.isPresent(HIBERNATE42_NOJTAPLATFORM_CLASS, null)) {
|
||||
jtaPlatform = ClassUtils.forName(HIBERNATE42_NOJTAPLATFORM_CLASS,
|
||||
null).newInstance();
|
||||
}
|
||||
if (jtaPlatform != null) {
|
||||
vendorProperties.put(JTA_PLATFORM, jtaPlatform);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Could not configure the JTA platform", e);
|
||||
}
|
||||
dunno(vendorProperties);
|
||||
}
|
||||
}
|
||||
|
||||
private void dunno(Map<String, Object> vendorProperties) throws LinkageError {
|
||||
JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
|
||||
if (jtaTransactionManager != null) {
|
||||
vendorProperties.put(JTA_PLATFORM, new SpringJtaPlatform(
|
||||
jtaTransactionManager));
|
||||
}
|
||||
else {
|
||||
vendorProperties.put(JTA_PLATFORM, getNoJtaPlatformManager());
|
||||
}
|
||||
}
|
||||
|
||||
private Object getNoJtaPlatformManager() {
|
||||
for (String noJtaPlatformClass : NO_JTA_PLATFORM_CLASSES) {
|
||||
try {
|
||||
return Class.forName(noJtaPlatformClass).newInstance();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Continue searching
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Could not configure JTA platform");
|
||||
}
|
||||
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
|
||||
static class HibernateEntityManagerCondition extends SpringBootCondition {
|
||||
|
||||
private static String[] CLASS_NAMES = {
|
||||
|
||||
@@ -45,8 +45,8 @@ import com.google.gson.Gson;
|
||||
* @author Piotr Maj
|
||||
* @author Oliver Gierke
|
||||
* @author David Liu
|
||||
* @author Sebastien Deleuze
|
||||
* @author Andy Wilkinson
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(HttpMessageConverter.class)
|
||||
|
||||
@@ -60,9 +60,9 @@ import static org.mockito.Mockito.verify;
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
* @author Sebastien Deleuze
|
||||
* @author Andy Wilkinson
|
||||
* @author Marcel Overdijk
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class JacksonAutoConfigurationTests {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.jersey;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -34,7 +32,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jersey.CustomServletPathTests.Application;
|
||||
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationCustomServletPathTests.Application;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
@@ -47,20 +45,28 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link JerseyAutoConfiguration} when using custom servlet paths.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@IntegrationTest("server.port=0")
|
||||
@WebAppConfiguration
|
||||
public class CustomServletPathTests {
|
||||
|
||||
public class JerseyAutoConfigurationCustomServletPathTests {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
|
||||
private RestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:" + port + "/rest/hello", String.class);
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity(
|
||||
"http://localhost:" + this.port + "/rest/hello", String.class);
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
}
|
||||
|
||||
@@ -71,10 +77,10 @@ public class CustomServletPathTests {
|
||||
|
||||
@Value("${message:World}")
|
||||
private String msg;
|
||||
|
||||
|
||||
@GET
|
||||
public String message() {
|
||||
return "Hello " + msg;
|
||||
return "Hello " + this.msg;
|
||||
}
|
||||
|
||||
public Application() {
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.jersey;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -33,7 +31,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jersey.DefaultServletPathTests.Application;
|
||||
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationDefaultServletPathTests.Application;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
@@ -46,11 +44,18 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link JerseyAutoConfiguration} when using default servlet paths.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@IntegrationTest("server.port=0")
|
||||
@WebAppConfiguration
|
||||
public class DefaultServletPathTests {
|
||||
public class JerseyAutoConfigurationDefaultServletPathTests {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
@@ -59,8 +64,8 @@ public class DefaultServletPathTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:"
|
||||
+ port + "/hello", String.class);
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity(
|
||||
"http://localhost:" + this.port + "/hello", String.class);
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
}
|
||||
|
||||
@@ -77,7 +82,7 @@ public class DefaultServletPathTests {
|
||||
|
||||
@GET
|
||||
public String message() {
|
||||
return "Hello " + msg;
|
||||
return "Hello " + this.msg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Reference in New Issue
Block a user