f78c1d25a62e510aa69d40ddc6e2294575b8db6b
[JavaForFun] /
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xmlns:mvc="http://www.springframework.org/schema/mvc"
5         xmlns:context="http://www.springframework.org/schema/context"
6         xmlns:util="http://www.springframework.org/schema/util"
7         xmlns:p="http://www.springframework.org/schema/p"
8         xsi:schemaLocation="
9                 http://www.springframework.org/schema/beans
10                 http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/mvc
12         http://www.springframework.org/schema/mvc/spring-mvc.xsd
13         http://www.springframework.org/schema/context
14         http://www.springframework.org/schema/context/spring-context.xsd
15         http://www.springframework.org/schema/util
16         http://www.springframework.org/schema/util/spring-util.xsd">
17    
18         <!--
19                 I am declaring my beans without the automatic annotation. :/
20                 Better because we are saving memory but it requires more configuration.
21                 
22                 See: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
23                 <mvc:annotation-driven/>
24          -->
25          
26    
27         <context:annotation-config />
28    
29         <context:component-scan base-package="de.spring.example.rest, org.resthub"/>
30         
31         <!--
32                 Required beans for generating XML responses from Java objects using JAXB annotations
33                 Jackson also works but it doesn't generate XML with namespaces... O.o
34                 
35                 This implementation will be slower than the one using Jackson :( but I am going to use it just for WADL generation :)
36         -->    
37     <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
38         <property name="packagesToScan" value="org.jvnet.ws.wadl"/>
39     </bean>
40         <bean id="jaxbConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
41         <constructor-arg ref="jaxbMarshaller" />
42         </bean>
43     
44         <!-- Required beans for generating JSON responses from Java objects -->
45     <bean id="jsonObjectMapperFactory" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
46         p:indentOutput="true" p:failOnEmptyBeans="false">
47         <property name="featuresToDisable">
48             <array>
49                 <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
50                 
51                 <!-- Useful when using Java 8 objects like OffsetDateTime.
52                          I want to keep the offset in time zone if it exists.
53                          
54                          LIKE THIS ONE: 2014-07-03 23:27:36+0100
55                 -->
56                 <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE"/>
57                 
58                 <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION"/>
59             </array>
60         </property>
61         <property name="modulesToInstall" ref="customJacksonModules"/>
62     </bean>
63     
64     <util:list id="messageConverters">
65         <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" p:objectMapper-ref="jsonObjectMapperFactory"/>
66                 <ref bean="jaxbConverter" />
67         <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
68     </util:list>
69
70
71         <bean name="handlerAdapter"
72                 class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
73                 <property name="webBindingInitializer">
74                         <bean
75                                 class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
76                                 <!-- It enables us to use JSR-303 -->
77                                 <property name="validator">
78                                         <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
79                                 </property>
80                         </bean>
81                 </property>
82                 <property name="messageConverters" ref="messageConverters" />
83                 
84                 
85                 <property name="requestBodyAdvice">
86                         <util:list>
87                                 <bean id="requestBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice"/>
88                         </util:list>
89                 </property>
90                 
91                 
92                 <property name="responseBodyAdvice">
93                         <util:list>
94                                 <bean id="responseBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice"/>
95                         </util:list>
96                 </property>
97         </bean>
98     
99         <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
100
101
102         <util:list id="customJacksonModules">
103                 <array>
104                         <value type="java.lang.Class">de.spring.example.rest.resource.modules.SpringJpaModule</value>
105                 </array>                
106         </util:list>
107         
108         <mvc:default-servlet-handler />
109         
110 </beans>