Java quick implementation of WebService (synchronization logic)

Source: Internet
Author: User
Tags webservice annotation

Today, we need to call some third-party fake services for testcase. This third party uses the soap WebService mode.

First, create a service.

Package myws; import Java. io. ioexception; import javax. JWS. webmethod; import javax. JWS. webparam; import javax. JWS. webresult; import javax. JWS. webService; import javax. XML. BIND. annotation. xmlaccesstype; import javax. XML. BIND. annotation. xmlaccessortype; import javax. XML. BIND. annotation. xmlrootelement; import javax. XML. WS. endpoint;/***** @ author account leader */@ WebService (name = "hello", targetnamespace = "http://chillyc.info/api", servicename = "API ", portname = "portname") public class webserviceholder {@ webmethod @ webresult (name = "return") Public String Hello (@ webparam (name = "name") string name) {return "hello" + name;} public static void main (string [] ARGs) throws ioexception {endpoint. publish ("http: // localhost: 80/fake/WS", new webserviceholder (); system. in. read ();}}

Here, we write system. In. Read (); in the hope that the service will be stuck there. Basically, all servers are similar to the endless loop. So I am so lazy to use io here.

Note that @ WebService annotation. name indicates the porttype. Targetnamespace has the same name in all subsequent calls. Servicename is the service name. Portname is actually the name of the port that provides the service (the call to the service itself is irrelevant ). If no name is available, Java uses webserviceholder by default as the class name.

After running, open it in the browser

http://localhost:80/fake/ws?wsdl
Then you can see the WSDL file.
This XML file does not appear to have any style information associated with it. The document tree is shown below.<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://chillyc.info/api" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://chillyc.info/api" name="API"><types><xsd:schema><xsd:import namespace="http://chillyc.info/api" schemaLocation="http://localhost/fake/ws?xsd=1"/></xsd:schema></types><message name="hello"><part name="parameters" element="tns:hello"/></message><message name="helloResponse"><part name="parameters" element="tns:helloResponse"/></message><message name="getReturnInfo"><part name="parameters" element="tns:getReturnInfo"/></message><message name="getReturnInfoResponse"><part name="parameters" element="tns:getReturnInfoResponse"/></message><portType name="HELLO"><operation name="hello"><input message="tns:hello"/><output message="tns:helloResponse"/></operation><operation name="getReturnInfo"><input message="tns:getReturnInfo"/><output message="tns:getReturnInfoResponse"/></operation></portType><binding name="PortNameBinding" type="tns:HELLO"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="hello"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation><operation name="getReturnInfo"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="API"><port name="PortName" binding="tns:PortNameBinding"><soap:address location="http://localhost/fake/ws"/></port></service></definitions>

Let's compare the names, servicename, and so on in the WSDL file. Then, write a stub as the called interface.

Package myws; import javax. JWS. webparam; import javax. JWS. webService;/***** @ author account before pawn */@ WebService (targetnamespace = "http://chillyc.info/api", name = "hello ") public interface webserviceapi {string Hello (@ webparam (name = "name") string name );}
Note that the hello function must be the same as the function name published by WebService (the name in the wdsl file is required .) In addition, the name in webparam must be the same as the parameter name in the publish function. Here, two values are input in WebService. Here, name is the name in WebService. It is actually the porttype in WSDL. If an error is entered here. The following error occurs: Undefined port type: {http://chillyc.info/api+name. So be careful. In addition, write an executable class.
Package myws; import java.net. malformedurlexception; import java.net. URL; import javax. XML. namespace. QNAME; import javax. XML. WS. service;/***** @ author before the account */public class client {public static void main (string [] ARGs) throws malformedurlexception {webserviceapi API = service. create (new URL ("http: // localhost: 80/fake/WS? WSDL "), new QNAME (" http://chillyc.info/api "," API ")). getport (webserviceapi. class); system. out. println (API. hello ("Sss "));}}

The URL here is the address of the WSDL file. QNAME is passed in to targetnamespace and servicename. In addition, getport is filled in with stub. Then you can directly call the interface in stub to get the result. Done. simple and fast. It takes 10 minutes to build and write the client. Of course, this is just the start

This method is applicable to JDK 6 and later versions. Other versions are unknown.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.