webService简介

webService是一个跨平台的可互操作性技术,他为第三方调用提供服务支持.
webService通过xml来描述数据,利用soap来作为信息交换的协议.
soap协议:是一个简单的基于xml的协议.他可以和许多因特网协议结合,包括纯文本协议Http等.

webService使用:1、请求的xml数据,
2、webService接口服务
3、服务返回的对象转化为xml格式

配置----相当于把服务给注册一下:
<bean id="userInfoService" class="wang.xin.hong.UserInfo/>"
<jaxws:endpoint id="any" implementor="#userInfoService" address="/UserInfoService" />

下面是基于jaxb解析xml文件及cxf完成的Webservice应用:

一、导入cxf、jaxb及spring的相关jar包
二、在web.xml中加入
<!-- webService配置引入 -->
<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>

三、webService搭建完成后,要对服务进行注册:
<?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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

<context:component-scan base-package="com"/>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>


<bean id="userDataService" class="com.ncs.jczy.wsdl.impl.UserImportServiceImpl"/>


<!-- webService 注册 bean的id引入-->
<jaxws:endpoint id="BaseDataUserendpoint" implementor="#userDataService" address="/userService"/>


<!--也可以直接写成以下配置

<jaxws:endpoint id="BaseDataUserendpoint" implementor="com.ncs.jczy.wsdl.impl.UserImportServiceImpl" address="/userService"/>

-->
</beans>

四、自定义客服端访问webService服务的参数格式
<?xml version="1.0" encoding="UTF-8"?>
<request>
<users>
<user>
<nickName>昵称</nickName>
<email>Email</email>
<password>密码</password>
<regDate>注册日期</regDate>
</user>
</users>
</request>

五、自定义服务处理完成后返回给客户端的结果格式:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<resultCode></resultCode>
<resultDesc></resultDesc>
<users>
<user>
<nickName></nickName>
<email></email>
<password></password>
<regDate></regDate>
<proErrList>
<pro name=""></pro>
<pro name=""></pro>
</proErrList>
</user>
</users>
</response>

六、配置和xml格式相互转换的java Bean(传入参数的转换)
package com.ncs.jczy.wsdl.dto;
//将客户单传入的xml参数格式转换为javaBean.
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.ncs.jczy.domain.User;

@XmlRootElement(name="request") //request.xml的根元素.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"users"})
public class UserRequestDto {

@XmlElementWrapper(name = "users")
@XmlElement(name = "user")
private List<User> users;

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}
}



package com.ncs.jczy.wsdl.dto;

//webService生成的结果对象,返回给客户端时要将其转化为xml格式.
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.ncs.jczy.domain.User;

@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"resultCode","resultDesc","depts"})
public class UserResponseDto {

@XmlElement(name = "resultCode")
private int resultCode;

@XmlElement(name = "resultDesc")
private String resultDesc;

@XmlElementWrapper(name = "users")
@XmlElement(name = "user")
private List<User> users;

public int getResultCode() {
return resultCode;
}

public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}

public String getResultDesc() {
return resultDesc;
}

public void setResultDesc(String resultDesc) {
this.resultDesc = resultDesc;
}

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

}


//request.xml中包含多个<user></user><user></user>,user需单独封装bean
package com.ncs.jczy.wsdl.bean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"nickName","email","password","regDate_text"})
public class UserVo {

@XmlElement(name = "nickName")
private String nickName;

@XmlElement(name = "email")
private String email;

@XmlElement(name = "password")
private String password;

@XmlElement(name = "regDate")
private String regDate_text;

public String getNickName() {
return nickName;
}

public void setNickName(String nickName) {
this.nickName = nickName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRegDate_text() {
return regDate_text;
}

public void setRegDate_text(String regDate_text) {
this.regDate_text = regDate_text;
}
}


七、javaBean对象与xml文件格式之间的装换(jaxb技术)
package com.ncs.jczy.utils.jaxb;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.binding.corba.wsdl.Exception;
import org.apache.cxf.binding.corba.wsdl.Object;
import org.apache.xpath.operations.String;
import org.springframework.oxm.Marshaller;

public class JaxbWrapper {
private static JAXBContext jaxbContext;

public JaxbWrapper(Class<?>... rootType){
try {
jaxbContext = JAXBContext.newInstance(rootType);
} catch (JAXBException e) {
e.printStackTrace();
}
}

/**
* xml转化为javaBean.
* @param <T>
* @param xml
* @return
*/
@SuppressWarnings("unchecked")
public <T> T fromXml(String xml) {
try {
StringReader reader = new StringReader(xml);
return (T) jaxbContext.createUnmarshaller().unmarshal(reader);
} catch (Exception e) {
throw new RuntimeException("xml转化为javaBean失败",e);
}
}

public String objectToXml(Object o,String encoding){
StringWriter writer = new StringWriter();
try {
createMarshaller(encoding).marshal(o, writer);
} catch (JAXBException e) {
e.printStackTrace();
}
return writer.toString();
}

private Marshaller createMarshaller(String encoding) throws JAXBException {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if(StringUtils.isNotBlank(encoding)){
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
}
}


八、具体的webService服务:
package com.ncs.jczy.wsdl.impl;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

import com.ncs.jczy.dao.UserDao;
import com.ncs.jczy.wsdl.BaseDataService;


/**
* webService基类.
* @author gaoli
*
*/
public abstract class BaseServiceImpl implements BaseDataService{

@Resource
private WebServiceContext ctx;

@Resource
protected UserDao userDao;

}


//webService具体的实现类.

package com.ncs.jczy.wsdl.impl;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.ncs.jczy.domain.User;
import com.ncs.jczy.utils.jaxb.JaxbWrapper;
import com.ncs.jczy.wsdl.BaseDataService;
import com.ncs.jczy.wsdl.dto.UserRequestDto;
import com.ncs.jczy.wsdl.dto.UserResponseDto;


@WebService(name = "BaseDataService",targetNamespace = "http://wxh4468.com/services",endpointInterface="com.ncs.jczy.wsdl.BaseDataService")
public class UserImportServiceImpl extends BaseServiceImpl implements BaseDataService{

public String dataImport(@WebParam(name = "dataXml") String dataXml) {

//生成的结果Bean
UserResponseDto responseDto = new UserResponseDto();

if(null != dataXml && !"".equals(dataXml)){

}else{
//设置属性,对应的xml文件格式.
responseDto.setResultCode(10401);
responseDto.setResultDesc("requst paramenter [dataXml] is null");
return new JaxbWrapper(UserResponseDto.class).objectToXml(responseDto,null);
}


//将客户端参数转化为javaBean.
UserRequestDto requestDto = new JaxbWrapper(UserRequestDto.class).fromXml(dataXml);

//处理好参数之后执行具体的操作.
if(null != requestDto.getUsers() && requestDto.getUsers().size() > 0){
List<User> users = requestDto.getUsers();

for(User u : users){
userDao.addUser(u);
}

}
return null;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值