java中jaxws:endpoint webservices笔记

 1.wsdl如下:

<?xml version="1.0" ?>- <wsdl:definitions name="UserService" targetNamespace="cn.hk.fs" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="cn.hk.fs" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="cn.hk.fs" xmlns:tns="cn.hk.fs" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="query" type="tns:query" /> 
- <xsd:complexType name="query">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="queryResponse" type="tns:queryResponse" /> 
- <xsd:complexType name="queryResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="update" type="tns:update" /> 
- <xsd:complexType name="update">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="userId" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="updateResponse" type="tns:updateResponse" /> 
- <xsd:complexType name="updateResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="queryResponse">
  <wsdl:part element="tns:queryResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="update">
  <wsdl:part element="tns:update" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="updateResponse">
  <wsdl:part element="tns:updateResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="query">
  <wsdl:part element="tns:query" name="parameters" /> 
  </wsdl:message>
- <wsdl:portType name="User">
- <wsdl:operation name="query">
  <wsdl:input message="tns:query" name="query" /> 
  <wsdl:output message="tns:queryResponse" name="queryResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="update">
  <wsdl:input message="tns:update" name="update" /> 
  <wsdl:output message="tns:updateResponse" name="updateResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="UserServiceSoapBinding" type="tns:User">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="query">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="query">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="queryResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="update">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="update">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="updateResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="UserService">
- <wsdl:port binding="tns:UserServiceSoapBinding" name="UserPort">
  <soap:address location="http://localhost:8085/ws/services/user" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>
 

2.application.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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://www.springframework.org/schema/tx 
  	http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/aop 
  	http://www.springframework.org/schema/aop/spring-aop.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"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-javascript-client.xml"/>              	
  	
	<!-- 用户接口 -->
	<bean id="user" class="cn.hk.User" />
	<!--  -->

	<bean id="customInterceptors" class="cn.interceptors.CustomInterceptors" />
	<jaxws:endpoint id="endpoint1" implementor="#user"  address="/user"   >
	<jaxws:inInterceptors>
				<ref bean="customInterceptors"/>
			</jaxws:inInterceptors>
	</jaxws:endpoint>
	 
	
</beans>

3.web.xml<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<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>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>
    
	<display-name>ws</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>		
	</welcome-file-list>
</web-app>
 

4.User.javapackage cn.hk;

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


@WebService(targetNamespace="cn.hk.fs")

public class User {
	
	//不作为webservices发布
	@WebMethod(exclude = true)
	public String add(String user,String password){
		return user+"-"+password;
	}
	//私有默认就是不发布
	@SuppressWarnings("unused")
	private void delete(){
		
		
	}
	//这个方法发布
	public String update(@WebParam(name = "userId")
		    String userId){
		
		return "ADMIN:"+userId;
	}
	
	public String query(String userId){
		return "query"+userId;
	}
	public User(){
		
	}
	public User(String user){
		
	}
	
}

5.客户端调用

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class Test {

public static void main(String[] args) {
invokeWsUser();
}

/**
* JAX-RPC调用
*/
public static void invokeWsUser() {
try {
String address = "http://localhost:8085/ws/services/user?wsdl";
String namespaceURI = "cn.hk.fs";
String methodName = "query";
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(address);
options.setTo(targetEPR);

// 指定print方法的传进的参数值

Object[] opAddEntryArgs = new Object[] { "123456" }; //
// 指定print方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 指定要调用的print方法及WSDL文件的命名空间
QName opAddEntry = new QName(namespaceURI, methodName);
// 调用print方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
		opAddEntryArgs, classes)[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 但是碰到个RPC调update方法,那个参数就传不进去啊。谁知道告诉我啊,谢谢了
}

/**
* http调用
*/
public static void invokeHttpWsUser() {

URL url;
try {
url = new URL("http://localhost:8085/ws/services/user?wsdl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
con.setRequestMethod("PUT");
con.setRequestProperty("Accept", "text/xml");
con.setRequestProperty("User-Agent", "JAX-WS RI 2.1.3-hudson-390-");
OutputStream ostream = con.getOutputStream();

/**
 * 请求头的xml格式 <soap:Envelope
 * xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 * xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * <soap:Body> <ns1:query
 * xmlns:ns1="http://schemas.xmlsoap.org/soap/http"> <arg0
 * xmlns="">123456</arg0> </ns1:query> </soap:Body> </soap:Envelope>
 */
String xml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soap:Body>
<ns1:query xmlns:ns1=\"http://schemas.xmlsoap.org/soap/http\"><arg0>"
		+ 123456
		+ "</arg0></ns1:query></soap:Body></soap:Envelope>";
/**
 * 注意这个arg0要和上面的wsdl中的query的xsd:element中的name一样
 * 如果调用update方法,就要写update的xsd:element中的name一样,userId
 * <xsd:complexType name="query"> <xsd:sequence> <xsd:element
 * minOccurs="0" name="arg0" type="xsd:string" /> </xsd:sequence>
 * </xsd:complexType>
 */
ostream.write(xml.getBytes());
ostream.flush();
ostream.close();
// con.setRequestProperty("userId", "12");

InputStream istream = con.getInputStream();
int x = -1;
String str = "";
byte[] b = new byte[1];
while ((x = istream.read(b)) != -1) {
	str += new String(b, "utf-8");

}
istream.close();
System.out.println(str);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// HttpConnection con=new HttpConnection("", port)
}
}

 

 6.更多axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端

http://flyzonemu.iteye.com/blog/987619
axis 文件上传和下载http://blog.csdn.net/cuisuqiang/article/details/7304487
http://www.phprpc.org/zh_CN/docs/  //这里有个web services强大的工具
http://cxf.apache.org/docs/jax-ws-configuration.html //jax-ws:endpoint configuration
创建WebService的几种方式简介(EndPoint、JAX-WS、CXF、axis2、自定义Servlet+Document解析) 一、什么是WebService 2013年初出校门,便开始了北漂之旅。闲暇时间就看程序方面的书籍,其中李刚老师的《疯狂XML讲义》也曾拜读过,那时便有了WebService的初步认知,Spring+Cxf或Spring+axis2也曾练过,未能在项目上大刀阔斧。直到2016年底,给甲方实施企业服务总线(ESB)的时候 ,开始各种天花乱坠的WebService服务、规范开始应用,也... 阅读详情

相关推荐

CXF 发布WebService - jaxws:endpoint

# 依赖 jar commons-logging-1.1.1.jar cxf-2.7.5.jar httpasyncclient-4.0-beta3.jar httpclient-4.2.1.jar httpcore-4.2.2.jar httpcore-nio-4.2.2.jar neethi-3.0.2.jar org.springframework.aop-3.1.3.REL

dyccsxg的专栏 1万+

Web Service学习之:CXF拦截器

一、用途 CXF拦截器类似Struts2的拦截器,后者是拦截和处理请求,前者是对发送和接收的sope消息进行处理,一般用于WS请求响应中的权限验证、日志记录,Soap消息处理,消息的压缩处理等; 这个拦截器可以直接访问和修改sope消息。 拿权限验证举例: 二、服务端添加拦截器 三种方式:JaxWsServerFactoryBean、Endpoint都可以通过getInInterceptors方法...

qq_33337186的博客 1472

Cxf - Spring集成Cxf暴露WebServices

一、Spring集成Cxf暴露Web Services     1、新建Web工程,导入Spring Core、Spring Web和Cxf的jar包     2、配置web.xml <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLS

袭冷 1285

WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)

作者:@展云 本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhanxiaoyun/p/7942902.html 目录 一、CXF与Spring整合(jaxws:endpoint形式配置) 1.新建一个maven项目 2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件二、客户端调...

a3289780的博客 1530

Webservices in JDK 6

Vivek Pandey ...

chouhuanlu1849的博客 151

JDK6中的WebServices

Java SE 5到Java SE 6中的一个变化就是:在Java SE6中引入了针对Web Services的java api。也就是说Web Services已经不单单是Java EE中的规范了,在Java SE6中也对WebServices的规范做了支持。只需安装了Java SDK 6后就可以进行Web Services程序的开发和编译了。其中包含的Web Services相关的主要规范如下:Name                                                  

ooflywing的专栏 1949

Java maven项目通过cxf框架发布WebServices接口,及jdk发布WebServices接口

package com.company; import javax.jws.WebMethod; import javax.jws.WebService; //使用@WebService注解标注WebServiceI接口 @WebService public interface WebServiceInter { //使用@WebMethod注解标注WebServiceI接口中的方法 @WebMethod String sayHello(String name);

qq_41606535的博客 687

WebServices客户端调用Server端https协议的Endpoint的Exception

JAX-WS webservices 客户端调用Server端,当Server端EndPoint 是https协议,老是报SSLHandshakeException,google了下,网上提示需要生成ssl 证书. 1. 下载InstallCert.java 文件 2. 执行ma...

chuangwu2528的博客 338

WebServices CXF开发常见异常及解决方法

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ===========

hgffhh的博客 1151

使用Apache CXF开发WebServices服务

  在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端。 但是如果你访问Apache的官网,可以看到xfire已经被合并了。 最新的框架叫做CXF。 Apache CXF = Celtix + XFire。 CXF 继承了 Celtix 和 XFire 两大开源项目的精华, 提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码

有情有义有朋友 8910

JAVA访问HTTPS协议的Web Service . jdk安裝ser方法

JAVA访问HTTPS协议的Web Service . jdk安裝ser方法   一朋友因工作需要,要访问HTTPS协议的Web Service,与其一同摸索,终于解决。 欣喜之余,记录下来供需要的网友参考。 1. 下载证书 访问服务的WSDL地址,比如 https://访问站点地址/nealnet-epcis/query?wsdl  浏览器会提示证书错误或此网

zerojunyan的专栏 815

java 1.6 webservice_java jdk1.6内置支持的webservice使用示例

webService是一种跨语言的系统间交互标准。在java中使用webservice根据服务器端的服务根据描述生成WSDL文件,并将应用与此WSDL文件一起放入HTTP服务器中,借助服务工具根据WSDL文件生成客户端STUB代码。此代码的作用是将产生的对象请求信息封装成标准的SOAP格式数据,并发送到服务器端,服务器端根据接收到的SOAP格式数据进行转换,并最终通过反射调用响应类的响应方法。jd...

weixin_33587161的博客 339

在eclipse中使用jax-ws构建webservices服务端和客户端

服务端: package com.yinfu.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class TestWebsService { public String sayHello(String username) { ...

weixin_34351321的博客 169

springboot2.1.3集成webservice及错误No operation was found with the name {...}解决办法

1.项目使用springboot 2.1.3版本,集成webservice使用的依赖如下 1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</...

weixin_30827565的博客 921

Java小白翻身 - webservice教程2

步骤 5 登陆接口实现类@WebService(serviceName = “LoginService”, // 与接口中指定的name一致targetNamespace = “http://java18.cn”, // 与接口中的命名空间一致endpointInterface = “com.webservice.demo.services.LoginService”// 接口地址@Override步骤 6 创建CXF配置类@Bean必看视频!

2301_82243799的博客 749

浅谈WebService的调用<转>

0.前言     前段时间,公司和电信有个合作,产品对接电信的某个平台,使用了WebService接口的调用,实现了业务受理以及单点登录。终于使用到了WebService,楼主还是比较兴奋的,目前功能已经上线,下面进行使用总结。WebService涉及到内容还是比较多的,涉及到发布和调用,有不少知识点,本文只是最简单的调用。 1.WebService简介     WebService是基于s...

weixin_33919941的博客 168
上一篇: Jquery BlockUI的使用
下一篇: CXF整合Spring之JaxWsProxyFactoryBean调用
tempdowncomputer
博客等级 码龄16年 2粉丝 405原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值