前面给大家分享了如何发布 http://blog.youkuaiyun.com/pref_mail/article/details/74399185
下面给大家分享下如何请求
1.也先说下本地环境 JDK1.8+MAVEN+NEXUS私服[这里需要用到]
2.创建工程 以及工程结构
3.web.xml 其实都是和服务端的信息是差不多的
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-cxf-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cxf_app</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 加载配置需要和Spring容器一起启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf_app</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
基本上就是把服务器的web.xml copy下
4.spring-cxf-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--
id:唯一标识
serviceClass:对应接口类型
address:对应发布的webService地址
-->
<jaxws:client id="tes" serviceClass="com.pref.webserverclent.TestWebService"
address="http://localhost:8081/ws/testWeb" />
</beans>
然后创建伪接口 .
a.下载官网的CXF安装包http://www.apache.org/dyn/closer.lua/cxf/3.1.12/apache-cxf-3.1.12.tar.gz
b.解压后放在本地
c.打开DOS命令行,切换到解压的盘 进入到\apache-cxf-3.1.11\bin\目录下 执行以下命令
得到本地调用代码 代码目录在
加入到本地工程中:
直接使用
现在我们就可以像使用本地代码那样直接使用了
@Autowired
private TestWebService testWebService;
@Override
public String getUser(String name) {
//调动webService
String namea =testWebService.show(name);
return namea;
}
这样就完成了 ,但是这样不太好玩,每次要求请求webService的时候 还要去生成伪代码….
转换发送的格式
在服务器端 我们将我们返回的参数 封装成一个对象,为此我们写个工具类
package com.pref.webservice.util;
import com.pref.bean.User;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.Date;
/**
* Created by Administrator on 2017/6/30/030.
*/
public class XMLUtil {
/**
* 对象转换XML
*
* @param c
* @param o
* @return
*/
public static String obj2Xml(Class c, Object o) {
JAXBContext context = null; // 获取上下文对象
try {
context = JAXBContext.newInstance(c);
Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码字符集
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进
// 打印到控制台
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(o, baos);
String xmlObj = new String(baos.toByteArray()); // 生成XML字符串
return xmlObj;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
/**
* xml 转换对象
* @param xml
* @param c
* @return
*/
public static Object xml2Object(String xml, Class c) {
try {
JAXBContext context = JAXBContext.newInstance();
Unmarshaller unmarshaller = context.createUnmarshaller();
Object obj = unmarshaller.unmarshal(new StringReader(xml));
return obj;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
User user = new User();
user.setCreateTime(new Date());
user.setUserPwd("1231");
user.setUserName("okdd");
user.setName("ddd");
String s = obj2Xml(User.class, user);
System.out.println(s);
}
}
在发布的webService中 调用
@Override
public String login(String name, String pwd) {
User user = new User();
user.setCreateTime(new Date());
user.setUserPwd("1231");
user.setUserName("okdd");
user.setName("ddd");
String xml = XMLUtil.obj2Xml(User.class,user);
return xml;
}
发布完成后 在请求端
public static void main(String[] args) {
String xml=CXFUtil.getRemortInfo("http://localhost:8081/ws/testWeb?wsdl","login","alf");
System.out.println(xml);
User user = (User) XMLUtil.xml2Object(xml, User.class);
System.out.println(user.getName());
}
这边是需要知道webService发布方的数据 在服务器上讲返回的数据发布为jar
set /p localJar=输入本地jar包全路径:
set /p localDgroupId=输入仓库DgroupId:
set /p localDartifactId=输入仓库DartifactId:
set /p localVersion=输入Jar版本:
mvn install:install-file -Dfile=loalJar -DgroupId=localDgroupId -DartifactId=localDartifactId -Dversion=localVersion -Dpackaging=jar
这样可以把jar 发布都本地仓库
在请求端
<!-- 自定义的jar -->
<dependency>
<groupId>com.pref.server</groupId>
<artifactId>server-bean</artifactId>
<version>1.0</version>
</dependency>
当然其实可以发布到私服中 具体细节可以百度下
这样就可以不用copy伪接口了
下载地址:
http://download.youkuaiyun.com/detail/pref_mail/9891808