-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKapetaDefaultConfig.java
More file actions
89 lines (74 loc) · 3.2 KB
/
KapetaDefaultConfig.java
File metadata and controls
89 lines (74 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2023 Kapeta Inc.
* SPDX-License-Identifier: MIT
*/
package com.kapeta.spring.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.kapeta.spring.config.pageable.PageableDeserializer;
import com.kapeta.spring.config.pageable.PageableSerializer;
import com.kapeta.spring.security.AuthorizationForwarderSupplier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.domain.Pageable;
/**
* Default configuration for kapeta
*/
public class KapetaDefaultConfig {
public static ObjectMapper createDefaultObjectMapper() {
var om = JsonMapper.builder()
.addModule(new JavaTimeModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build();
SimpleModule pageableModule = new SimpleModule();
pageableModule.addSerializer(Pageable.class, new PageableSerializer());
pageableModule.addDeserializer(Pageable.class, new PageableDeserializer(om));
om.registerModule(pageableModule);
return om;
}
/**
* Is needed to support @Value("${some.config.property}")
*/
@Bean
@ConditionalOnMissingBean(PropertySourcesPlaceholderConfigurer.class)
public PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
/**
* A default object mapper that can be used by other modules.
* <p>
* Will use sensible defaults for JSON serialization/deserialization that makes it easy to work with
* <p>
* See {@link #createDefaultObjectMapper()} for details
*/
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper objectMapper() {
return createDefaultObjectMapper();
}
/**
* Provides a way to forward authorization headers to another service
* This is used by the REST Client SDK to automatically forward JWT to other internal services
*/
@Bean
@ConditionalOnMissingBean(AuthorizationForwarderSupplier.class)
public AuthorizationForwarderSupplier authorizationForwarderSupplier() {
return () -> null;
}
}