面向服务的架构
异构系统间的远程调用
远程调用
面向过程 c语言
面向对象 c++ java
面向组件 DCOM EJB (相同语言之间)
面向标准组件 web service
面向服务 soa
webService 简单实现
需要注意的是服务器端的包名,要和客户端的包名相同(命名空间必须相同)
示例代码如下:
接口类:
package cn.yue.servicetest;
import javax.jws.WebService;
/**
* 定义webService接口
*
* @time 5:50:58 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@WebService
public interface MyService {
public String seyHello(String name);
}
实现类:
package cn.yue.servicetest.impl;
import cn.yue.servicetest.MyService;
/**
* webService实现类
*
* @time 5:52:55 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MyServiceImpl implements MyService {
public String seyHello(String name) {
return "你好" + name;
}
}
web Service 发布到tomcat服务器上
需要spring来集成
spring 的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- cxf内部使用的一些bean的定义 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- 指定跟哪种协议进行绑定 http, soap -->
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!-- servlet的bean定义 -->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="myService" class="cn.yue.servicetest.impl.MyServiceImpl" />
<!-- 服务端 -->
<jaxws:server address="/myService">
<jaxws:serviceBean>
<ref bean="myService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- =============== Spring 设置 =============== -->
<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>
<!-- =============== CXF 设置 =============== -->
<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>
<!-- 定义所有以services打头的路径是要访问web服务 -->
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
使用自带工具生成java类型
bin目录下的wsdl2java -d 目标目录 路径(格式如下)
http://localhost:8080/webServiceDemo/services/myService?wsdl
webservice 工作原理
向服务器端发送一段xml文件,报务器端向客户端返回一个xml文件,尊守soap协议(简单对象访问协议)
依赖http协议,不受访火墙影响
复杂返加类型
jaxb定义了xml与java对象之间的转换
在实体bean上添加注解@XmlRootElement
xml 与java对象之间的转换(java ---- schema )
schema 的一般格式
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/myservice"
xmlns:tns="http://www.example.org/myservice"
elementFormDefault="qualified">
<element name="customer" type="tns:customerType">
</element>
<!-- 定义customer 类型 -->
<complexType name="customerType" >
<sequence>
<element name="id" type="int"/>
<element name="name" type="string"/>
</sequence>
</complexType>
</schema>
使用jdk自带的xjc生成java类文件
命令如下:
xjc -d 目标目录 源文件目录
在生成的java类中,内个常用的注解
@XmlAccessorType(XmlAccessType.FIELD)
PROPERTY 公有的get set 方法
FIELD 私有属性映射
PUBLIC_MEMBER 根据公有属性和公有的get set方法进行映射
java转xml实现代码如下:
public static void main(String[] args) throws JAXBException {
CustomerType type = new CustomerType();
type.setId(4);
type.setName("赵六");
JAXBElement<CustomerType> element = new ObjectFactory().createCustomer(type);
// 转换工厂
JAXBContext context = JAXBContext.newInstance(CustomerType.class);
// java转xml
Marshaller marshaller = context.createMarshaller();
//中文乱码问题(控制台默认输出编码为gbk)
marshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");
marshaller.marshal(element, System.out);
}
xml转java
webService学习笔记
最新推荐文章于 2025-05-03 10:56:02 发布