WebService(1)

新建web项目,然后创建一个WEB服务:

@WebService()
public class Hello {
    @WebMethod(operationName 
= "sayHi")
    
public String sayHi(@WebParam(name = "name")String name) {
       
return "Hi " + name;
    }

}


可以在源图上右键,选Web服务--添加操作,也可以在设计图上直接添加操作。@WebService标注表明该类是一个web服务,展现给web服务客户端的业务方法必须使用@WebMethod标注来表示。打包部署该web应用,web服务自动会发布。可以在glassfish应用服务器上找到该web服务,直接测试或者查看服务器生成的WSDL

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice/" name="HelloService">
    
<types>
        
<xsd:schema>
            
<xsd:import namespace="http://webservice/" schemaLocation="http://localhost:8080/WebServiceApp/HelloService?xsd=1">
            
</xsd:import>
        
</xsd:schema>
    
</types>
    
<message name="sayHi">
        
<part name="parameters" element="tns:sayHi">
        
</part>
    
</message>
    
<message name="sayHiResponse">
        
<part name="parameters" element="tns:sayHiResponse">
        
</part>
    
</message>
    
<portType name="Hello">
        
<operation name="sayHi">
            
<input message="tns:sayHi">
            
</input>
            
<output message="tns:sayHiResponse">
            
</output>
        
</operation>
    
</portType>
    
<binding name="HelloPortBinding" type="tns:Hello">
        
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document">
        
</soap:binding>
        
<operation name="sayHi">
            
<soap:operation soapAction="">
            
</soap:operation>
            
<input>
                
<soap:body use="literal">
                
</soap:body>
            
</input>
            
<output>
                
<soap:body use="literal">
                
</soap:body>
            
</output>
        
</operation>
    
</binding>
    
<service name="HelloService">
        
<port name="HelloPort" binding="tns:HelloPortBinding">
            
<soap:address location="http://localhost:8080/WebServiceApp/HelloService">
            
</soap:address>
        
</port>
    
</service>
</definitions>


也可以编写客户端测试,新建一个普通的java项目,在项目上右键,选择新建--Web服务客户端,在弹出窗口中指定WebService项目或者WSDL url,点击完成。在源代码上右键,选择Web服务客户端资源--调用Web服务操作,在弹出窗口中选择sayHi操作,点确定,测试代码自动生成:

public class Main {

    
public static void main(String[] args) {

        
try 

            webservice.HelloService service 
= new webservice.HelloService();
            webservice.Hello port 
= service.getHelloPort();
            
            java.lang.String name 
= "Tom";
            java.lang.String result 
= port.sayHi(name);
            System.out.println(
"Result = " + result);
        }
 catch (Exception ex) {
            
// TODO handle custom exceptions here
        }

    }

}


运行该客户端,结果将会输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值