jackson jaxb annotations support in Spring - spring

i'm looking for the simplest way of adding jaxb annotations support to jackson.
Jackson is added now to Spring by <mvc:annotation-driven/>. I need that by #ResponseBody annotation the Object is converted to xml or json dependently to the media type.
I'm new in spring-mvc so doesn't understand well yet how things work. Thanks.

Okay, I assume you want to be able to return both XML and JSON. To do this you need to create MessageConverters for both formats.
The XML message converter:
<bean id="xmlConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<oxm:jaxb2-marshaller id="jaxb2Marshaller">
<!-- you must either bind your JAXB annotated classes here -->
<!-- OR provide a jaxb.index and use contextPath -->
<oxm:class-to-be-bound name="com.mycompany.MyClass"/>
</oxm:jaxb2-marshaller>
</constructor-arg>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="xml"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
The JSON message converter, which uses the JAXB annotations:
<bean id="jaxbAnnotationInspector"
class="org.codehaus.jackson.xc.JaxbAnnotationIntrospector"/>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
<property name="annotationIntrospector" ref="jaxbAnnotationInspector"/>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
<bean ref="jacksonObjectMapper"/>
</property>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
And finally, the AnnotationMethodHandlerAdapter, which will convert the responses to the appropriate content type, depending upon the accept headers:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="xmlConverter"/>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
Note that the JAXB support in jackson isn't 100% complete or correct all the time, but the developers are really good at fixing bugs and responding to error reports.

Related

Spring Batch - Load properties from database table

I have a requirement in my spring batch where I have to load few key value properties from a database table. Is this possible? The job runs in a stand alone environment and not in a container.
Please let me know if you have a solution for this. `I am in a secured environment and that is the reason I have not shared the code initially. Anyways below is what I have. Below are my properties
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:EnvConfig.properties</value>
<value>classpath:DatabaseConfig.properties</value>
<value>classpath:WebServiceConfig.properties</value>
</list>
</property>
</bean>
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="properties" />
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<bean class="org.apache.commons.configuration.ConfigurationConverter"
factory-method="getProperties">
<constructor-arg>
<bean class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg>
<ref bean="sam-datasource" />
</constructor-arg>
<constructor-arg value="PTTMCDB.PROPERTY" /> <!-- DB Table -->
<constructor-arg value="PROPERTYNAME" /> <!-- DB Key Column -->
<constructor-arg value="PROPERTYVALUE" /> <!-- DB Value Column -->
</bean>
</constructor-arg>
</bean>
</property>
</bean>
This is where I am reading the properties fetched from DB.
<bean id="emailReaderUtil"
class="mailreader.pop3.EmailReaderUtil">
<property name="popServerHost" value="${pop3.popServerHost}"/>
</bean>

Access spring property in jsp or javascript

I have a pageSize variable configured in spring properties. I need to access this pageSize property in almost all the jsps. What is the best way to get hold of this spring property.
src\main\resources\web.properties contains default.page.items.size=10
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
I know how to access the property in the controller but as this property is been accessed my mutliple pages hence i want some way to access it javascript or jsp directly
I used this in a old project (not sure if still works) in your dispatcher context declare a bean like:
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
and then, if using standard JSTL view resolver:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="exposedContextBeanNames">
<list>
<value>myProps</value>
</list>
</property>
</bean>
You should be able to access the properties inside JSP using ${myProps.XXX}

how to implement customized LdapAuthoritiesPopulator

This is some part of my spring-security.xml.
My requirement is - i want to use embedded LDAP server only and want to use LdapAuthoritiesPopulator with itt
<security:authentication-manager>
<security:ldap-authentication-provider
user-search-filter="(uid={0})"
user-search-base="ou=users"
group-search-filter="(uniqueMember={0})"
group-search-base="ou=groups"
group-role-attribute="cn"
role-prefix="ROLE_">
</security:ldap-authentication-provider>
</security:authentication-manager>
<!-- Use an embedded LDAP server. We need to declare the location of the LDIF file
We also need to customize the root attribute default is -->
<security:ldap-server ldif="classpath:mojo.ldif" root="dc=springframework,dc=org"/>
I want to use my custom LdapAuthoritiesPopulator.
How to use it with embedded ldap server.
I am new to spring as of now.
You can configure your authentication provider with DefaultLDAPAuthoritesPopulator and provide the details to find the groups for roles. If there is something more specific to your case, you could extend this class. Looking at the spring-security-samples. I am new to spring-security too but i found the source code to be very helpful. Good luck.
FYI
contextSource bean DefaultSpringSecurityContextSource should be configured with the url to your LDAP server.
Hope this helps.
<bean id="ldapAuthenticationProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<constructor-arg>
<bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<constructor-arg ref="contextSource"/>
<property name="userSearch">
<bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0" value="ou=Google,ou=People"/>
<constructor-arg index="1" value="(uid={0})"/>
<constructor-arg index="2" ref="contextSource"/>
</bean>
</property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<constructor-arg ref="contextSource"/>
<constructor-arg value="ou=Groups"/>
<property name="groupSearchFilter" value="member={0}"/>
<property name="searchSubtree" value="true"/>
</bean>
</constructor-arg>
<property name="authoritiesMapper">
<bean class="org.springframework.security.core.authority.mapping.SimpleAuthorityMapper">
<property name="convertToUpperCase" value="true"/>
</bean>
</property>
</bean>
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://127.0.0.1:389/dc=google,dc=com"/>
</bean>

Why is "annotatedClasses" needed if there is #Entity?

Hello I'm building spring-hibernate application. Do i realy need configuration from below ?
<property name="annotatedClasses">
<list>
<value>org.fixus.springer.model.User</value>
</list>
</property>
I've set annotation-driven in my root-context.xml
<mvc:annotation-driven />
<context:component-scan base-package="org.fixus.springer" />
<context:component-scan base-package="org.fixus.springer.model" />
Now shouldn't hibernate automaticly take everything from this packages with annotation #Entity and convert it to table ? As for now without annotatedClasses he won't create a table from a entity
Use the docs, Luke!
[...]Example for an AnnotationSessionFactoryBean bean definition:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>test.package.Foo</value>
<value>test.package.Bar</value>
</list>
</property>
</bean>
Or when using classpath scanning for autodetection of entity classes:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="test.package"/>
</bean>
As you can see you have a choice between defining all classes explicitly or only the package for scanning. <context:component-scan/> does not recognize Hibernate/JPA annotations and hence has no effect.

Issue with Spring Method interceptor

I'm having an issue with using Spring interceptor. I've a CXF service endpoint method which I'm trying a wrap with an interceptor to do some initialization. For some reason, the interceptor is not being invoked. Here's my spring entry:
<jaxrs:server id="acadConnectServer" address="/rest/acadconnect3">
<jaxrs:serviceBeans>
<ref bean="acadConnectResource" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="acadConnectResource"
class="com.test.connectchannel.service.AcadConnectChannelResource" />
<bean id="connectResource" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="acadConnectResource" />
<property name="interceptorNames">
<list>
<value>methodPointCut</value>
</list>
</property>
</bean>
<bean id="methodPointCut"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<ref local="methodInterceptor" />
</property>
<property name="mappedNames">
<list>
<value>search</value>
<value>searchJSONP</value>
</list>
</property>
</bean>
<bean id="methodInterceptor"
class="com.test.connectchannel.util.ConnectChannelInterceptor">
</bean>
As you can see, I've a CXF endpoint class AcadConnectChannelResource which has couple of methods search and searchJSONp. I've created the Named Method Cut interceptor to intercept these two method calls and so some initialization using the custom intercetor class.But, everytime the methods are invoked, the interceptor is not being called.
Not sure what I'm missing here, any pointer will be appreciated.
-Thanks
I might be entirely wrong, but don't you want
<ref bean="connectResource" />
instead of
<ref bean="acadConnectResource" />
in your <jaxrs:server>? You're proxying the resource, but using the raw resource instead of the proxy.

Resources