HibernateTemplate.save() method is not inserting in DB - spring

I am new to Spring and Hibernate. I am just trying to insert a new record in my table using Hibernate Spring Integration. Please see below code
My beans.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mainClient" class="Client.MainClient">
<property name="contactDetailsDao" ref="contactDetailsBean"></property>
</bean>
<bean id="contactDetailsBean" class="DAO.ContactDetailsDAO">
<property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
</bean>
<bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactoryBean"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceBean"></property>
<property name="mappingResources" value="ContactDetails.xml"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/SpringLearnDatabase"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
My hibernate.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping">
<hibernate-mapping>
<class name="ContactDetailsDTO.ContactDetailsDTO" table="contactDetails">
<id name="contactId" column="id" type="string"></id>
<property name="name" column="Name" type="string"></property>
<property name="mobileNumber" column="MobileNumber" type="string"></property>
<property name="emailId" column="EmailId" type="string"></property>
</class>
</hibernate-mapping>
My DAO class :
package DAO;
import java.util.List;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import ContactDetailsDTO.ContactDetailsDTO;
public class ContactDetailsDAO {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void insertIntoContact(ContactDetailsDTO cd){
try{
hibernateTemplate.save(cd);
}catch(Exception e){
e.printStackTrace();
}
}
public List<ContactDetailsDTO> getAllAccounts(){
return (List<ContactDetailsDTO>) hibernateTemplate.find("from ContactDetailsDTO");
}
}
When I try to getAllAccounts I can see select query in console and records from table, but when I try to insert new record in table, insert query is not displayed on console and new record is not inserted in table.
save() method is not inserting new record. Please check where I am wrong and reply
Thanks

Related

Exception in running spring hibernate application

Hi I Have implemented the spring and hibernate integration i have an exception while integrating the exceptions is:
Please Help Me to Solve This exception:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.InsertTest.main(InsertTest.java:14)
here is the xml file :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<bean id="d" class="test.EmployeeDao">
<property name="template" ref="template"></property>
</bean>
</beans>
here is the sample program:
>
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class InsertTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Resource r=new ClassPathResource("test.xml");
// BeanFactory factory=new XmlBeanFactory(r);
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDao dao=(EmployeeDao)context.getBean("d");
Employee e=new Employee();
e.setId(114);
e.setName("varun");
e.setSalary(50000);
dao.saveEmployee(e);
}
}
Just Remove The All Added JAR files and try Adding each Jar file Carefully wont add the already added jar files this will work i have tested and working for mine

hibernatetemplate.getSessionFactory() throws NullPointerException

My DaoClass
#Repository("genObj")
public class GeneralQueries {
HibernateTemplate hibernatetemplate;
public HibernateTemplate getHibernatetemplate() {
return hibernatetemplate;
}
public void setHibernatetemplate(HibernateTemplate hibernatetemplate) {
this.hibernatetemplate = hibernatetemplate;
}
public String getStringfromQuery(String sql)
{
SessionFactory sessionFactory=hibernatetemplate.getSessionFactory();
Session session=sessionFactory.openSession();
String data=null;
try
{
System.out.println(sql);
data=session.createSQLQuery(sql).list().toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return data;
}}
This method returns data as string
My Controller Class
#Controller
public class SchoolStudentsConfirmationContrl
{
#Autowired
SchoolStudentsConfirmationIntr schoolstdconfirmservice;
#Autowired
GeneralQueries genObj=new GeneralQueries();
#RequestMapping(value="/getData",method=RequestMethod.GET)
public ModelAndView getData(#ModelAttribute("schooldetailsform")SchoolDetailsForm formbean,HttpServletRequest request)
{
String PageHeading = "";
try
{
String district = request.getSession().getAttribute("dist_code").toString();
PageHeading = "BAS Students Confirmation for the Academic Year:"+ formbean.getAc_year() + " <br> District:"
+ genObj.getStringfromQuery("select dist_name from pmss_districts_mst where dist_code=" + district + "")+"";
mav.setViewName("showreportwithmenu");
}
catch(Exception e)
{
e.printStackTrace();
}
return mav;
}
}
Im trying to call the genObj.getStringfromQuery() method but it throws me null pointer exception at line
SessionFactory sessionFactory=hibernatetemplate.getSessionFactory();
my config file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="cgg.gov.in.*" annotation-config="true"/>
<bean id="tiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles3.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://x.x.x.x/test" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
</bean>
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" name="sessionFactory">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.autocommit">false</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>cgg.gov.in.model.login.LoginForm</value>
</list>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateTemplate" name="hibernatetemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
</beans>
You have a hibernatetemplate bean defined. But you haven't tell to point the variable, hibernatetemplate in GeneralQueries class, to the defined bean.
You can do this in two ways,
1) Create a bean for GeneralQueries in xml and define the property as below,
<bean name="generalQueries" class="package.GeneralQueries">
<property name="hibernatetemplate" ref="hibernatetemplate" />
</bean>
You have already defined the setter. Remember to remove #Repository from GeneralQueries, if you define it as a bean in xml.
2) Autowire the hibernatetemplate in GeneralQueries as below.
#Repository("genObj")
public class GeneralQueries {
#Autowired
HibernateTemplate hibernatetemplate;
//rest of code
}
GeneralQueries must be under component-scan.
Note: Also, #m-deinum suggested, remove new GeneralQueries() from SchoolStudentsConfirmationContrl.

Hibernate integration with Spring

I have a problem about integrating Hibernate with Spring:
My hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/CODEL</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/model/Contact.hbm.xml" />
</session-factory>
</hibernate-configuration>
My applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="contactdao" class="com.dao.ContactDAO" scope="prototype" />
<bean id="userdao" class="com.dao.UserDAO" scope="prototype" />
<bean id="groupdao" class="com.dao.GroupDAO" scope="prototype" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/CODEL"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<!-- chemin vers le fichier hibernate de config. ça évite d'avoir un composant
datasource -->
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" timeout="-1" />
<tx:method name="sav*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.dao.ContactDAO.*(..))"
advice-ref="txAdvice" />
</aop:config>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="mySessionFactory"></property>
<property name="checkWriteOperations" value="false" />
</bean>
<bean id="contactDAO" class="com.dao.ContactDAO">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
</beans>
My dao
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.model.Contact;
import com.model.Entreprise;
import com.model.PhoneNumber;
import com.util.HibernateUtil;
public class ContactDAO extends HibernateDaoSupport {
private HibernateTemplate hibernateTemplate;
public ContactDAO() {
}
public void setHibernateTemplate(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public List<Contact> listAllContacts() {
List<Contact> listContacts = (List<Contact>) this.hibernateTemplate
.find("from CODEL.Contact");
return listContacts;
}
}
I've got an error NullPointerException at the line of this.hibernateTemplate.find... By println, I've seen it null. Thanks for any suggestion and explanation why my code produced this error.
Can you please update your ContactDAO setHibernateTemplate method to set the hibernateTemplate, instead of creating a new HibernateTemplate, for example
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
currently your setter method is taking sessionFactory, so its not calling the actual setter for hibernate template, which could be causing the null pointer exception.
Let's say you have to tell Spring how different components communicate between them.You need annotations to linked them together.
The easiest way is letting Spring detects these links itself.
So in applicationContext.xml, add this line of code :
<context:component-scan base-package="com.example.pkg" />
Then, Spring will search for every components in this package.
You need also declare the existence of your comopents.
#Repository(value = "contactDAO")
public class ContactDAO extends HibernateDaoSupport
And you have to link the bean created in context to your class attribut:
#Autowired
private HibernateTemplate hibernateTemplate;
Then you can use this HibernateTemplate object without setMethod because it initialization is done by Spring.

org.hibernate.HibernateException: save is not valid without active transaction

I have a problem to save the data with hibernate from Spring.
When I save data get error:
Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
https://gist.github.com/RuzievBakhtiyor/f3009dbc6a9c31090b59
Spring beans config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<import resource="hibernate.xml"/>
<import resource="dataSource.xml"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:component-scan base-package="neron" />
Hibernate config bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="neron.models" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
DataSource config bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456789" />
</bean>
</beans>
TestDao:
package neron.dao.Impl;
import neron.dao.TestDao;
import neron.models.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
#Transactional
#Repository("testDao")
public class TestDaoImpl implements TestDao {
SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession()
{
return (Session)sessionFactory.getCurrentSession();
}
public void save(Test test) {
getCurrentSession().save(test);
}
public Test findById(int id) {
return (Test) getCurrentSession().get(Test.class,id);
}
}
You need to do db update operation (insert, update, delete) within transaction boundary .
Session session = getCurrentSession();
Transaction trans = session.beginTransaction(); //begin transaction
//db operation
session.save(test)
trans.commit(); //end transaction
OR
For annotation support, in your spring config bean, add this
<tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true" />
Remove this tag from sessionFactory -> hibernateProperties
<property name="hibernate.current_session_context_class">thread</property>

Spring xml in response via JAXB

I am using spring 3.1, and my application is already set up to send and receive data in json format. Now I need to provide one request API that should return the same data but in xml format.
Please help me with this stuff or tell what I am doing wrong. I tried JAXB, but instead of xml I receive "406 Not Acceptable".
My requests API:
/**
* Gets objects in json format
*/
#RequestMapping(value = "/objects/json", method = RequestMethod.GET)
#ResponseBody
public List<MyObjectTO> getAll() {
List<MyObjectTO> objectsList = new ArrayList<MyObjectTO>();
//forming objects
return objectsList;
}
/**
* Gets objects in xml format
*/
#RequestMapping(value = "/objects/xml", method = RequestMethod.GET,headers={"Accept=application/xml"})
#ResponseBody
public ResponseList getAll() {
ResponseList objectsList = new ResponseList ();
//the same formation
return objectsList;
}
Context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.kenshoo.urlbuilder"/>
<mvc:annotation-driven/>
<mvc:view-controller path="/mainpage" view-name="mainpage"/>
<util:properties id="addProps" location="classpath:config/addProps.properties"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
<entry key="pdf" value="application/pdf"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</beans>
My changes: Added message converters to AnnotationMethodHandlerAdapter and inserted JAXB marshaller. But after this got response "406 Not Acceptable":
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true"/>
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
</list>
</property>
</bean>
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.ResponseList</value>
</list>
</property>
</bean>
I would appreciate any help, thanks.
UPDATE
ResponseList structure:
public class ResponseList {
private List<FirstLevel> firstLevelObjects;
public List<FirstLevel> getFirstLevelObjects() {
return firstLevelObjects;
}
public void setFirstLevelObjects(List<FirstLevel> firstLevelObjects) {
this.firstLevelObjects= firstLevelObjects;
}
}
FirstLevel structure:
public class FirstLevel {
List<SecondLevel> secondLevelObjects;
boolean isConditional;
String beforeStart;
ConditionType type;//enum object
//...getters and setters
}
I lost hope with Castor, so tried once more with JAXB. Changed post
with details for JAXB. The same issues - I got Not Acceptable when
trying to request xml. Can you help me?
All you should need to do for JAXB is to add an #XmlRootElement annotation to your ResponseList class.
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class ResponseList {
private List<FirstLevel> firstLevelObjects;
public List<FirstLevel> getFirstLevelObjects() {
return firstLevelObjects;
}
public void setFirstLevelObjects(List<FirstLevel> firstLevelObjects) {
this.firstLevelObjects= firstLevelObjects;
}
}

Resources