Skip to main content

Serving Data

Server-side data access is covered by Spring and configured by Spring Boot in a few quite simple steps. Spring Data uses repository abstraction to implement Domain Driven Design approach and to minimize the boilerplate code required for Data Access Objects layer.

More documentation on this is available at official Spring Data resource.

Spring Configuration

The configuration for Spring Data is as follows:

build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
}

Namely, there are two parts of a larger Spring Data family are configured above, they are Spring Data JPA and Spring Data REST. Also, the third starter is responsible for Spring Web Application with embedded servlet container and additional REST capabilities. The latter is used for the cases then business domain logic with several repositories has to be implemented.

Configuring CORS

Since there are two Spring Web technologies are utilized, Cross-Origin Resource Sharing (CORS) should be configured twice.

CORS for Spring Web

This configuration is done with a configuration bean, like below:

src/main/java/org/clematis/*/config/SpringWebCorsConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class SpringWebCorsConfig {

public static final String ALL_REGEXP = "/**";
public static final String ORIGINS = "*";

@Bean(name = "corsConfigurationSource")
public CorsConfigurationSource corsConfigurationSource() {

CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of(ORIGINS));
configuration.setAllowedMethods(List.of(HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.PATCH.name(),
HttpMethod.DELETE.name(),
HttpMethod.OPTIONS.name(),
HttpMethod.HEAD.name()));

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(ALL_REGEXP, configuration);
return source;
}
}

CORS for Spring Data REST

Another configuration works with Spring Data REST endpoints:

src/main/java/org/clematis/*/config/SpringDataRestCorsConfig.java
package org.clematis.cosmic.config;

import org.clematis.cosmic.model.InputData;
import org.clematis.cosmic.model.Project;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

@Component
public class SpringDataRestCorsConfig implements RepositoryRestConfigurer {

public static final String ALL_REGEXP = "/**";
public static final String ORIGINS = "*";

@SuppressWarnings("checkstyle:MagicNumber")
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {

cors.addMapping(ALL_REGEXP)
.allowedOrigins(ORIGINS)
.allowedMethods(HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.PATCH.name(),
HttpMethod.DELETE.name(),
HttpMethod.OPTIONS.name(),
HttpMethod.HEAD.name())
.allowCredentials(false)
.maxAge(3600);

config.exposeIdsFor(Project.class);
config.exposeIdsFor(InputData.class);
}
}