我想使用泽西创建一个宁静的网络服务。我有一个由spring + maven + hibernate制作的现有项目。我想使用Hibernate的CRUD方法来创建我的Web服务。
这是我的服务类的代码:
@Component
public class MotServices {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public Mot getMotByNom(String nom) {
Mot mot = new Mot();
// Acquire session
Session session = sessionFactory.getCurrentSession();
try {
String queryString = "from Mot where nom = :nom";
Query query = session.createQuery(queryString);
query.setString("nom", nom);
mot = (Mot) query.uniqueResult();
} catch (RuntimeException e) {
e.printStackTrace();
}
System.out.println(mot);
return mot;
}
}
这是我的webservice的代码:
@Path("/hello")
public class HelloWS {
@Resource(name="motservices")
private MotServices motservices;
Mot mot = new Mot();
public MotServices getMotservices() {
return motservices;
}
public void setMotservices(MotServices motservices) {
this.motservices = motservices;
}
public Mot getMot()
{
return mot;
}
public void setMot(Mot mot) {
this.mot = mot;
}
@GET
@Path("/Mot1/mot/{param}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Mot getMot(@PathParam("param") String param) {
try{
mot = motservices.getMotByNom(param);
// mot = motservices.Recherccher("Arbeit", "");
}
catch(NullPointerException e)
{
mot = new Mot("motAAAA","motBBBB", "motCCCC");
}
return mot;
}
}
但是当我通过此链接调用webservice时的结果 " http://localhost:8080/Worterbuch/rest/hello/Mot1/mot/Arbeit" 我只有一个空值;结果就像这样
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<mot>
<definition>motBBBB</definition>
<example>motCCCC</example>
<id>0</id>
<nom>motAAAA</nom>
</mot>
你知道吗?
这是applicationcontext-web.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:context="http://www.springframework.org/schema/context"
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">
<!-- This file contains all the imports to other Spring application context
files. -->
<import resource="classpath:spring/applicationContext.xml" />
<context:annotation-config />
<context:component-scan base-package="com.zied.langue.controllers, com.zied.langue.WS" />
</beans>