Create spring-boot-data-rest module
This commit is contained in:
committed by
Phillip Webb
parent
de2827e23f
commit
41befa4fea
27
spring-boot-project/spring-boot-data-rest/build.gradle
Normal file
27
spring-boot-project/spring-boot-data-rest/build.gradle
Normal file
@@ -0,0 +1,27 @@
|
||||
plugins {
|
||||
id "java-library"
|
||||
id "org.springframework.boot.auto-configuration"
|
||||
id "org.springframework.boot.configuration-properties"
|
||||
id "org.springframework.boot.deployed"
|
||||
id "org.springframework.boot.optional-dependencies"
|
||||
}
|
||||
|
||||
description = "Spring Boot Data REST"
|
||||
|
||||
dependencies {
|
||||
api(project(":spring-boot-project:spring-boot-webmvc"))
|
||||
api(project(":spring-boot-project:spring-boot-jackson"))
|
||||
api("org.springframework.data:spring-data-rest-webmvc")
|
||||
|
||||
optional(project(":spring-boot-project:spring-boot-autoconfigure"))
|
||||
|
||||
testImplementation(project(":spring-boot-project:spring-boot-data-jpa"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-test"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
||||
testImplementation(testFixtures(project(":spring-boot-project:spring-boot-autoconfigure")))
|
||||
testImplementation("jakarta.servlet:jakarta.servlet-api")
|
||||
|
||||
testRuntimeOnly("ch.qos.logback:logback-classic")
|
||||
testRuntimeOnly("com.h2database:h2")
|
||||
testRuntimeOnly("com.zaxxer:HikariCP")
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.autoconfigure;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
|
||||
* integration.
|
||||
* <p>
|
||||
* Activates when the application is a web application and no
|
||||
* {@link RepositoryRestMvcConfiguration} is found.
|
||||
* <p>
|
||||
* Once in effect, the auto-configuration allows to configure any property of
|
||||
* {@link RepositoryRestConfiguration} using the {@code spring.data.rest} prefix.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Stephane Nicoll
|
||||
* @author Andy Wilkinson
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@AutoConfiguration(after = JacksonAutoConfiguration.class)
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
|
||||
@ConditionalOnClass(RepositoryRestMvcConfiguration.class)
|
||||
@EnableConfigurationProperties(RepositoryRestProperties.class)
|
||||
@Import(RepositoryRestMvcConfiguration.class)
|
||||
@SuppressWarnings("removal")
|
||||
public class RepositoryRestMvcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public SpringBootRepositoryRestConfigurer springBootRepositoryRestConfigurer(
|
||||
ObjectProvider<Jackson2ObjectMapperBuilder> objectMapperBuilder, RepositoryRestProperties properties) {
|
||||
return new SpringBootRepositoryRestConfigurer(objectMapperBuilder.getIfAvailable(), properties);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.autoconfigure;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Configuration properties for Spring Data REST.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.data.rest")
|
||||
public class RepositoryRestProperties {
|
||||
|
||||
/**
|
||||
* Base path to be used by Spring Data REST to expose repository resources.
|
||||
*/
|
||||
private String basePath;
|
||||
|
||||
/**
|
||||
* Default size of pages.
|
||||
*/
|
||||
private Integer defaultPageSize;
|
||||
|
||||
/**
|
||||
* Maximum size of pages.
|
||||
*/
|
||||
private Integer maxPageSize;
|
||||
|
||||
/**
|
||||
* Name of the URL query string parameter that indicates what page to return.
|
||||
*/
|
||||
private String pageParamName;
|
||||
|
||||
/**
|
||||
* Name of the URL query string parameter that indicates how many results to return at
|
||||
* once.
|
||||
*/
|
||||
private String limitParamName;
|
||||
|
||||
/**
|
||||
* Name of the URL query string parameter that indicates what direction to sort
|
||||
* results.
|
||||
*/
|
||||
private String sortParamName;
|
||||
|
||||
/**
|
||||
* Strategy to use to determine which repositories get exposed.
|
||||
*/
|
||||
private RepositoryDetectionStrategies detectionStrategy = RepositoryDetectionStrategies.DEFAULT;
|
||||
|
||||
/**
|
||||
* Content type to use as a default when none is specified.
|
||||
*/
|
||||
private MediaType defaultMediaType;
|
||||
|
||||
/**
|
||||
* Whether to return a response body after creating an entity.
|
||||
*/
|
||||
private Boolean returnBodyOnCreate;
|
||||
|
||||
/**
|
||||
* Whether to return a response body after updating an entity.
|
||||
*/
|
||||
private Boolean returnBodyOnUpdate;
|
||||
|
||||
/**
|
||||
* Whether to enable enum value translation through the Spring Data REST default
|
||||
* resource bundle.
|
||||
*/
|
||||
private Boolean enableEnumTranslation;
|
||||
|
||||
public String getBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
public void setBasePath(String basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
public Integer getDefaultPageSize() {
|
||||
return this.defaultPageSize;
|
||||
}
|
||||
|
||||
public void setDefaultPageSize(Integer defaultPageSize) {
|
||||
this.defaultPageSize = defaultPageSize;
|
||||
}
|
||||
|
||||
public Integer getMaxPageSize() {
|
||||
return this.maxPageSize;
|
||||
}
|
||||
|
||||
public void setMaxPageSize(Integer maxPageSize) {
|
||||
this.maxPageSize = maxPageSize;
|
||||
}
|
||||
|
||||
public String getPageParamName() {
|
||||
return this.pageParamName;
|
||||
}
|
||||
|
||||
public void setPageParamName(String pageParamName) {
|
||||
this.pageParamName = pageParamName;
|
||||
}
|
||||
|
||||
public String getLimitParamName() {
|
||||
return this.limitParamName;
|
||||
}
|
||||
|
||||
public void setLimitParamName(String limitParamName) {
|
||||
this.limitParamName = limitParamName;
|
||||
}
|
||||
|
||||
public String getSortParamName() {
|
||||
return this.sortParamName;
|
||||
}
|
||||
|
||||
public void setSortParamName(String sortParamName) {
|
||||
this.sortParamName = sortParamName;
|
||||
}
|
||||
|
||||
public RepositoryDetectionStrategies getDetectionStrategy() {
|
||||
return this.detectionStrategy;
|
||||
}
|
||||
|
||||
public void setDetectionStrategy(RepositoryDetectionStrategies detectionStrategy) {
|
||||
this.detectionStrategy = detectionStrategy;
|
||||
}
|
||||
|
||||
public MediaType getDefaultMediaType() {
|
||||
return this.defaultMediaType;
|
||||
}
|
||||
|
||||
public void setDefaultMediaType(MediaType defaultMediaType) {
|
||||
this.defaultMediaType = defaultMediaType;
|
||||
}
|
||||
|
||||
public Boolean getReturnBodyOnCreate() {
|
||||
return this.returnBodyOnCreate;
|
||||
}
|
||||
|
||||
public void setReturnBodyOnCreate(Boolean returnBodyOnCreate) {
|
||||
this.returnBodyOnCreate = returnBodyOnCreate;
|
||||
}
|
||||
|
||||
public Boolean getReturnBodyOnUpdate() {
|
||||
return this.returnBodyOnUpdate;
|
||||
}
|
||||
|
||||
public void setReturnBodyOnUpdate(Boolean returnBodyOnUpdate) {
|
||||
this.returnBodyOnUpdate = returnBodyOnUpdate;
|
||||
}
|
||||
|
||||
public Boolean getEnableEnumTranslation() {
|
||||
return this.enableEnumTranslation;
|
||||
}
|
||||
|
||||
public void setEnableEnumTranslation(Boolean enableEnumTranslation) {
|
||||
this.enableEnumTranslation = enableEnumTranslation;
|
||||
}
|
||||
|
||||
public void applyTo(RepositoryRestConfiguration rest) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(this::getBasePath).to(rest::setBasePath);
|
||||
map.from(this::getDefaultPageSize).to(rest::setDefaultPageSize);
|
||||
map.from(this::getMaxPageSize).to(rest::setMaxPageSize);
|
||||
map.from(this::getPageParamName).to(rest::setPageParamName);
|
||||
map.from(this::getLimitParamName).to(rest::setLimitParamName);
|
||||
map.from(this::getSortParamName).to(rest::setSortParamName);
|
||||
map.from(this::getDetectionStrategy).to(rest::setRepositoryDetectionStrategy);
|
||||
map.from(this::getDefaultMediaType).to(rest::setDefaultMediaType);
|
||||
map.from(this::getReturnBodyOnCreate).to(rest::setReturnBodyOnCreate);
|
||||
map.from(this::getReturnBodyOnUpdate).to(rest::setReturnBodyOnUpdate);
|
||||
map.from(this::getEnableEnumTranslation).to(rest::setEnableEnumTranslation);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.autoconfigure;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
|
||||
/**
|
||||
* A {@code RepositoryRestConfigurer} that applies configuration items from the
|
||||
* {@code spring.data.rest} namespace to Spring Data REST. Also, if a
|
||||
* {@link Jackson2ObjectMapperBuilder} is available, it is used to configure Spring Data
|
||||
* REST's {@link ObjectMapper ObjectMappers}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Order(0)
|
||||
@SuppressWarnings("removal")
|
||||
class SpringBootRepositoryRestConfigurer implements RepositoryRestConfigurer {
|
||||
|
||||
private final Jackson2ObjectMapperBuilder objectMapperBuilder;
|
||||
|
||||
private final RepositoryRestProperties properties;
|
||||
|
||||
SpringBootRepositoryRestConfigurer(Jackson2ObjectMapperBuilder objectMapperBuilder,
|
||||
RepositoryRestProperties properties) {
|
||||
this.objectMapperBuilder = objectMapperBuilder;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
|
||||
this.properties.applyTo(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
|
||||
if (this.objectMapperBuilder != null) {
|
||||
this.objectMapperBuilder.configure(objectMapper);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for Spring Data REST.
|
||||
*/
|
||||
package org.springframework.boot.data.rest.autoconfigure;
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.boot.data.rest.autoconfigure.RepositoryRestMvcAutoConfiguration
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.autoconfigure;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.data.jpa.autoconfigure.JpaRepositoriesAutoConfiguration;
|
||||
import org.springframework.boot.data.rest.domain.city.City;
|
||||
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
|
||||
import org.springframework.boot.jdbc.autoconfigure.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.jpa.autoconfigure.hibernate.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
|
||||
import org.springframework.data.rest.webmvc.BaseUri;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class RepositoryRestMvcAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigServletWebApplicationContext context;
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultRepositoryConfiguration() {
|
||||
load(TestConfiguration.class);
|
||||
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithCustomBasePath() {
|
||||
load(TestConfiguration.class, "spring.data.rest.base-path:foo");
|
||||
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
URI expectedUri = URI.create("/foo");
|
||||
assertThat(bean.getBasePath()).as("Custom basePath not set").isEqualTo(expectedUri);
|
||||
BaseUri baseUri = this.context.getBean(BaseUri.class);
|
||||
assertThat(expectedUri).as("Custom basePath has not been applied to BaseUri bean").isEqualTo(baseUri.getUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithCustomSettings() {
|
||||
load(TestConfiguration.class, "spring.data.rest.default-page-size:42", "spring.data.rest.max-page-size:78",
|
||||
"spring.data.rest.page-param-name:_page", "spring.data.rest.limit-param-name:_limit",
|
||||
"spring.data.rest.sort-param-name:_sort", "spring.data.rest.detection-strategy=visibility",
|
||||
"spring.data.rest.default-media-type:application/my-json",
|
||||
"spring.data.rest.return-body-on-create:false", "spring.data.rest.return-body-on-update:false",
|
||||
"spring.data.rest.enable-enum-translation:true");
|
||||
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
assertThat(bean.getDefaultPageSize()).isEqualTo(42);
|
||||
assertThat(bean.getMaxPageSize()).isEqualTo(78);
|
||||
assertThat(bean.getPageParamName()).isEqualTo("_page");
|
||||
assertThat(bean.getLimitParamName()).isEqualTo("_limit");
|
||||
assertThat(bean.getSortParamName()).isEqualTo("_sort");
|
||||
assertThat(bean.getRepositoryDetectionStrategy()).isEqualTo(RepositoryDetectionStrategies.VISIBILITY);
|
||||
assertThat(bean.getDefaultMediaType()).isEqualTo(MediaType.parseMediaType("application/my-json"));
|
||||
assertThat(bean.returnBodyOnCreate(null)).isFalse();
|
||||
assertThat(bean.returnBodyOnUpdate(null)).isFalse();
|
||||
assertThat(bean.isEnableEnumTranslation()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithCustomConfigurer() {
|
||||
load(TestConfigurationWithConfigurer.class, "spring.data.rest.detection-strategy=visibility",
|
||||
"spring.data.rest.default-media-type:application/my-json");
|
||||
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
assertThat(bean.getRepositoryDetectionStrategy()).isEqualTo(RepositoryDetectionStrategies.ALL);
|
||||
assertThat(bean.getDefaultMediaType()).isEqualTo(MediaType.parseMediaType("application/my-custom-json"));
|
||||
assertThat(bean.getMaxPageSize()).isEqualTo(78);
|
||||
}
|
||||
|
||||
@Test
|
||||
void backOffWithCustomConfiguration() {
|
||||
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.base-path:foo");
|
||||
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
assertThat(bean.getBasePath()).isEqualTo(URI.create(""));
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigServletWebApplicationContext applicationContext = new AnnotationConfigServletWebApplicationContext();
|
||||
applicationContext.setServletContext(new MockServletContext());
|
||||
applicationContext.register(config, BaseConfiguration.class);
|
||||
TestPropertyValues.of(environment).applyTo(applicationContext);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(EmbeddedDataSourceConfiguration.class)
|
||||
@ImportAutoConfiguration({ HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class,
|
||||
JacksonAutoConfiguration.class })
|
||||
static class BaseConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@TestAutoConfigurationPackage(City.class)
|
||||
@EnableWebMvc
|
||||
static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Import({ TestConfiguration.class, TestRepositoryRestConfigurer.class })
|
||||
static class TestConfigurationWithConfigurer {
|
||||
|
||||
}
|
||||
|
||||
@Import({ TestConfiguration.class, RepositoryRestMvcConfiguration.class })
|
||||
static class TestConfigurationWithRestMvcConfig {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@TestAutoConfigurationPackage(City.class)
|
||||
@EnableWebMvc
|
||||
@SuppressWarnings({ "removal", "deprecation" })
|
||||
static class TestConfigurationWithObjectMapperBuilder {
|
||||
|
||||
@Bean
|
||||
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder objectMapperBuilder() {
|
||||
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder objectMapperBuilder = new org.springframework.http.converter.json.Jackson2ObjectMapperBuilder();
|
||||
objectMapperBuilder.simpleDateFormat("yyyy-MM");
|
||||
return objectMapperBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestRepositoryRestConfigurer implements RepositoryRestConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
|
||||
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategies.ALL);
|
||||
config.setDefaultMediaType(MediaType.parseMediaType("application/my-custom-json"));
|
||||
config.setMaxPageSize(78);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.domain.city;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class City implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String state;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String country;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String map;
|
||||
|
||||
protected City() {
|
||||
}
|
||||
|
||||
public City(String name, String country) {
|
||||
this.name = name;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return this.country;
|
||||
}
|
||||
|
||||
public String getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + "," + getState() + "," + getCountry();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.data.rest.domain.city;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CityRepository extends JpaRepository<City, Long> {
|
||||
|
||||
@Override
|
||||
Page<City> findAll(Pageable pageable);
|
||||
|
||||
Page<City> findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country, Pageable pageable);
|
||||
|
||||
City findByNameAndCountryAllIgnoringCase(String name, String country);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user