结合Spring、CXF等框架,以注解的方式实现WebService。
在作业五(Quartz任务调度)基础上,实现在一个WebService,该服务用于根据蛋糕的id查询蛋糕价格。
要求:
1.编写服务器端实现WebService,提交web.xml及相关类文件。
2.编写客户端,调用WebService,输出结果,只提交类文件即可。
服务端:
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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<description>Spring公共配置 </description>
<!-- 配置Spring上下文的注解 -->
<context:annotation-config />
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan
base-package="com.lrf.cakeonline">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 属性文件位置 -->
<context:property-placeholder
location="classpath:*.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 要扫描的实体类所在的包 -->
<property name="packagesToScan"
value="com.lrf.cakeonline.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.useUnicode">${hibernate.useUnicode}</prop>
<prop key="hibernate.characterEncoding">${hibernate.characterEncoding}</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- 使用annotation定义事务,事务管理器配置, Hibernate单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 任务调度 -->
<task:annotation-driven />
<jaxws:endpoint id="wsUtil"
implementor="com.lrf.cakeonline.ws.WebServiceUtil" address="/wsutil" />
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Homework06</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
WebServiceUtil.java:
package com.lrf.cakeonline.ws;
import javax.annotation.Resource;
import javax.jws.WebService;
import com.lrf.cakeonline.cake.service.CakeService;
@WebService
public class WebServiceUtil {
@Resource
private CakeService cakeService;
public int getCakePriceByCakeId(int cakeId) {
return this.cakeService.getCakePriceById(cakeId);
}
}
CakeDao.java:
package com.lrf.cakeonline.cake.dao;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
@Repository
public class CakeDao {
@Resource
private SessionFactory sessionFactory;
public int getPriceById(int cakeId) {
Query query = this.sessionFactory.getCurrentSession().createQuery("select price from Cake where id=:id");
query.setParameter("id", cakeId);
return ((Number)query.uniqueResult()).intValue();
}
}
CakeService.java:
package com.lrf.cakeonline.cake.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lrf.cakeonline.cake.dao.CakeDao;
@Service
@Transactional(readOnly = true)
public class CakeService {
@Resource
private CakeDao cakeDao;
public int getCakePriceById(int cakeId) {
return this.cakeDao.getPriceById(cakeId);
}
}
客户端:
Test.java:
package com.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Test {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/Homework06/services/wsutil?wsdl");
try {
Object[] objs = client.invoke("getCakePriceByCakeId", 4);
System.out.println("蛋糕价格为:" + objs[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}