16
16
17
17
package org .springframework .boot .autoconfigure .hateoas ;
18
18
19
+ import javax .annotation .PostConstruct ;
20
+
19
21
import org .springframework .beans .BeansException ;
22
+ import org .springframework .beans .factory .BeanFactory ;
23
+ import org .springframework .beans .factory .BeanFactoryAware ;
24
+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
20
25
import org .springframework .beans .factory .annotation .Autowired ;
26
+ import org .springframework .beans .factory .annotation .Qualifier ;
21
27
import org .springframework .beans .factory .config .BeanPostProcessor ;
22
28
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
23
29
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
27
33
import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
28
34
import org .springframework .boot .autoconfigure .web .HttpMessageConvertersAutoConfiguration ;
29
35
import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
36
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
30
37
import org .springframework .context .annotation .Bean ;
31
38
import org .springframework .context .annotation .Configuration ;
32
39
import org .springframework .hateoas .EntityLinks ;
33
40
import org .springframework .hateoas .LinkDiscoverers ;
41
+ import org .springframework .hateoas .RelProvider ;
34
42
import org .springframework .hateoas .Resource ;
35
43
import org .springframework .hateoas .config .EnableEntityLinks ;
36
44
import org .springframework .hateoas .config .EnableHypermediaSupport ;
37
45
import org .springframework .hateoas .config .EnableHypermediaSupport .HypermediaType ;
46
+ import org .springframework .hateoas .hal .CurieProvider ;
47
+ import org .springframework .hateoas .hal .Jackson2HalModule ;
38
48
import org .springframework .http .converter .json .Jackson2ObjectMapperBuilder ;
39
49
import org .springframework .plugin .core .Plugin ;
40
50
import org .springframework .web .bind .annotation .RequestMapping ;
55
65
@ ConditionalOnWebApplication
56
66
@ AutoConfigureAfter ({ WebMvcAutoConfiguration .class , JacksonAutoConfiguration .class ,
57
67
HttpMessageConvertersAutoConfiguration .class })
68
+ @ EnableConfigurationProperties (HateoasProperties .class )
58
69
public class HypermediaAutoConfiguration {
59
70
60
71
@ Configuration
@@ -65,40 +76,90 @@ protected static class HypermediaConfiguration {
65
76
@ ConditionalOnClass ({ Jackson2ObjectMapperBuilder .class , ObjectMapper .class })
66
77
protected static class HalObjectMapperConfiguration {
67
78
79
+ @ Autowired
80
+ private HateoasProperties hateoasProperties ;
81
+
68
82
@ Autowired (required = false )
69
- private Jackson2ObjectMapperBuilder objectMapperBuilder ;
83
+ private CurieProvider curieProvider ;
84
+
85
+ @ Autowired
86
+ @ Qualifier ("_relProvider" )
87
+ private RelProvider relProvider ;
88
+
89
+ @ Autowired (required = false )
90
+ private ObjectMapper primaryObjectMapper ;
91
+
92
+ @ PostConstruct
93
+ public void configurePrimaryObjectMapper () {
94
+ if (this .primaryObjectMapper != null
95
+ && this .hateoasProperties .isApplyToPrimaryObjectMapper ()) {
96
+ registerHalModule (this .primaryObjectMapper );
97
+ }
98
+ }
99
+
100
+ private void registerHalModule (ObjectMapper objectMapper ) {
101
+ objectMapper .registerModule (new Jackson2HalModule ());
102
+ Jackson2HalModule .HalHandlerInstantiator instantiator = new Jackson2HalModule .HalHandlerInstantiator (
103
+ HalObjectMapperConfiguration .this .relProvider ,
104
+ HalObjectMapperConfiguration .this .curieProvider );
105
+ objectMapper .setHandlerInstantiator (instantiator );
106
+ }
70
107
71
108
@ Bean
72
- public BeanPostProcessor halObjectMapperConfigurer () {
73
- return new BeanPostProcessor () {
74
-
75
- @ Override
76
- public Object postProcessAfterInitialization (Object bean ,
77
- String beanName ) throws BeansException {
78
- if (HalObjectMapperConfiguration .this .objectMapperBuilder != null
79
- && bean instanceof ObjectMapper
80
- && "_halObjectMapper" .equals (beanName )) {
81
- HalObjectMapperConfiguration .this .objectMapperBuilder
82
- .configure ((ObjectMapper ) bean );
83
- }
84
- return bean ;
85
- }
86
-
87
- @ Override
88
- public Object postProcessBeforeInitialization (Object bean ,
89
- String beanName ) throws BeansException {
90
- return bean ;
91
- }
92
-
93
- };
109
+ public static HalObjectMapperConfigurer halObjectMapperConfigurer () {
110
+ return new HalObjectMapperConfigurer ();
94
111
}
112
+
95
113
}
114
+
96
115
}
97
116
98
117
@ Configuration
99
118
@ ConditionalOnMissingBean (EntityLinks .class )
100
119
@ EnableEntityLinks
101
120
protected static class EntityLinksConfiguration {
121
+
102
122
}
103
123
124
+ /**
125
+ * {@link BeanPostProcessor} to apply any {@link Jackson2ObjectMapperBuilder}
126
+ * configuration to the HAL {@link ObjectMapper}.
127
+ */
128
+ private static class HalObjectMapperConfigurer implements BeanPostProcessor ,
129
+ BeanFactoryAware {
130
+
131
+ private BeanFactory beanFactory ;
132
+
133
+ @ Override
134
+ public Object postProcessBeforeInitialization (Object bean , String beanName )
135
+ throws BeansException {
136
+ if (bean instanceof ObjectMapper && "_halObjectMapper" .equals (beanName )) {
137
+ postProcessHalObjectMapper ((ObjectMapper ) bean );
138
+ }
139
+ return bean ;
140
+ }
141
+
142
+ private void postProcessHalObjectMapper (ObjectMapper objectMapper ) {
143
+ try {
144
+ Jackson2ObjectMapperBuilder builder = this .beanFactory
145
+ .getBean (Jackson2ObjectMapperBuilder .class );
146
+ builder .configure (objectMapper );
147
+ }
148
+ catch (NoSuchBeanDefinitionException ex ) {
149
+ // No Jackson configuration required
150
+ }
151
+ }
152
+
153
+ @ Override
154
+ public Object postProcessAfterInitialization (Object bean , String beanName )
155
+ throws BeansException {
156
+ return bean ;
157
+ }
158
+
159
+ @ Override
160
+ public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
161
+ this .beanFactory = beanFactory ;
162
+ }
163
+
164
+ }
104
165
}
0 commit comments