-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathWebMvcConfig.java
More file actions
123 lines (112 loc) · 5.63 KB
/
Copy pathWebMvcConfig.java
File metadata and controls
123 lines (112 loc) · 5.63 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* The MIT License
* Copyright © 2017 FAIR Data Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.fairdatateam.fairdatapoint.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.fairdatateam.fairdatapoint.api.converter.ErrorConverter;
import org.fairdatateam.fairdatapoint.api.converter.RdfConverter;
import org.jspecify.annotations.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverters;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.json.JsonMapper;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final List<ErrorConverter> errorConverters;
private final List<RdfConverter> rdfConverters;
/**
* Constructor (autowired)
*/
public WebMvcConfig(List<ErrorConverter> errorConverters, List<RdfConverter> rdfConverters) {
this.errorConverters = errorConverters;
this.rdfConverters = rdfConverters;
}
/**
* Configures message converters. See
* <a href="https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-config/message-converters.html">
* example in Spring docs</a>.
*/
@Override
public void configureMessageConverters(HttpMessageConverters.@NonNull ServerBuilder builder) {
builder.withStringConverter(new StringHttpMessageConverter())
.withJsonConverter(new JacksonJsonHttpMessageConverter(jsonMapper()))
.addCustomConverter(new ByteArrayHttpMessageConverter());
errorConverters.forEach(builder::addCustomConverter);
rdfConverters.forEach(builder::addCustomConverter);
}
@Override
public void configureContentNegotiation(@NonNull ContentNegotiationConfigurer configurer) {
for (ErrorConverter converter : errorConverters) {
converter.configureContentNegotiation(configurer);
}
for (RdfConverter converter : rdfConverters) {
converter.configureContentNegotiation(configurer);
}
configurer.favorParameter(true);
}
@Bean
@Primary
public JsonMapper jsonMapper() {
return JsonMapper.builder()
// https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.9.8/com/fasterxml/jackson/databind/ObjectMapper.html#findAndRegisterModules--
// https://github.com/FasterXML/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md#objectmapper-serialization-inclusion-configuration
.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL))
.changeDefaultPropertyInclusion(incl -> incl.withContentInclusion(JsonInclude.Include.NON_NULL))
// https://github.com/FasterXML/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md#objectmapper-visibility-configuration
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
}
@Bean
public InternalResourceViewResolver defaultViewResolver() {
return new InternalResourceViewResolver();
}
/**
* Global Cross-Origin Resource Sharing (CORS) configuration based on
* <a href="https://docs.spring.io/spring-framework/reference/web/webmvc-cors.html#mvc-cors-global">
* Web MVC CORS example</a>
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE")
.allowedHeaders(
HttpHeaders.ORIGIN,
HttpHeaders.AUTHORIZATION,
HttpHeaders.ACCEPT,
HttpHeaders.CONTENT_TYPE
)
// If the response contains these headers, the browser will expose them in JavaScript
.exposedHeaders(HttpHeaders.LOCATION, HttpHeaders.LINK);
}
}