Java calls webservice in four ways: javawebservice

Source: Internet
Author: User
Tags sendmsg

Java calls webservice in four ways: javawebservice
Webservice:
Cross-language calls between applications
Wwww.webxml.com.cn
1. xml
2. wsdl: webservice description language web Service description language
The xml format is used to describe how to call the address method. You can refer to the error webservice instruction manual.

3. soap simple object access protoacl (simple object access Protocol)
The xml format is limited.
Soap transmits xml data based on http (because there is a request body, it must be a post request)
The xml format of the request and response is as follows: <Envelop>
<Body>
//....
</Body>
</Envelop>
Operation name: method provided by the Service


Static methods cannot be published as external services

Use the jkd built-in code to generate the client code for accessing the server E:/wsimort-s. http://test.cm /? Wsdl

We can regard webservice as an application on the web server, and web server as a container of webservice.

Function Parameters in http://test.cm /? Xsd = 1

JAX-WS refers to java api for xml-WebService

// Test the WebService explorer
Web Service Explorer displays the returned xml format

By default, targetNamespace is the inverted package name.

The method by which the client calls WebService:
1. generate code through wximport
2. client-side Programming
3. ajax call
4. Calling through URL Connection


Request Process Analysis:
1. get the wsdl file using the get method, which is called a handshake.
2. Use post to send a request

3. Successful Server Response


Several listening tools:
Http watch
Web Service explorer
Eclipse built-in tool TCP/IP Monitor


Server code:

<Span style = "font-size: 18px;"> package com. webservcie; import javax. jws. webMethod; import javax. jws. webParam; import javax. jws. webResult; import javax. jws. webService; import javax. xml. ws. endpoint;/*** WebService ** mark Java classes as Web Services, or mark Java interfaces as Web Service interfaces */@ WebService (serviceName = "MyService ", targetNamespace = "http://www.baidu.com") public class HelloService {@ WebMethod (operationName = "AliassayHello") @ WebResult (name = "myReturn ") public String sayHello (@ WebParam (name = "name") String name) {return "hello:" + name;} public String sayGoodbye (String name) {return "goodbye: "+ name ;}@ WebMethod (exclude = true) // The current method is not published public String sayHello2 (String name) {return" hello "+ name ;} public static void main (String [] args) {/*** parameter 1: service publishing address * parameter 2: Service implementer * Endpoint restarts a thread */Endpoint. publish ("http://test.cm/", new HelloService (); System. out. println ("Server ready... ") ;}}</span>

1. Client call (wximport automatically generates code [recommended ])

<Span style = "font-size: 18px;"> package com. wsclient; public class App {/*** use wsimport to parse the wsdl and generate client code to call the WebService Service ** @ param args **/public static void main (String [] args) {// TODO Auto-generated method stub/*** <service name = "MyService"> * get service name */MyService mywebService = new MyService (); /*** <port name = "HelloServicePort" binding = "tns: HelloServicePortBinding"> */HelloService hs = mywebService. getHelloServicePort ();/*** call Method */System. out. println (hs. sayGoodbye ("sjk"); System. out. println (hs. aliassayHello ("sjk") ;}</span>
2. Call through ajax + js + xml

<Html> 

3. URL Connection

Import java. io. inputStream; import java. io. outputStream; import java.net. httpURLConnection; import java.net. URL;/*** call Webservice through UrlConnection **/public class App {public static void main (String [] args) throws Exception {// service URL wsUrl = new URL ("http: // 192.168.1.100: 6789/hello"); HttpURLConnection conn = (HttpURLConnection) wsUrl. openConnection (); conn. setDoInput (true); conn. setDoOutput (tr Ue); conn. setRequestMethod ("POST"); conn. setRequestProperty ("Content-Type", "text/xml; charset = UTF-8"); OutputStream OS = conn. getOutputStream (); // Request body String soap = "<soapenv: Envelope xmlns: soapenv = \" inline "xmlns: q0 = \" http://ws.itcast.cn/\ "xmlns: xsd = \ "http://www.w3.org/2001/XMLSchema\" xmlns: xsi = \ "http://www.w3.org/2001/XMLSchema-instance\"> "+" <soapenv: Body> <Q0: sayHello> <arg0> aaa </arg0> </q0: sayHello> </soapenv: Body> </soapenv: Envelope> "; OS. write (soap. getBytes (); InputStream is = conn. getInputStream (); byte [] B = new byte [1024]; int len = 0; String s = ""; while (len = is. read (B ))! =-1) {String ss = new String (B, 0, len, "UTF-8"); s + = ss;} System. out. println (s); is. close (); OS. close (); conn. disconnect ();}}

4. single client programming (same as the first method)

// File name: HelloService. javaimport javax. jws. webMethod; import javax. jws. webParam; import javax. jws. webResult; import javax. jws. webService; import javax. xml. bind. annotation. xmlSeeAlso; import javax. xml. ws. requestWrapper; import javax. xml. ws. responseWrapper;/*** This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.6 in JDK 6 * Generated source version: 2.1 ***/@ WebService (name = "HelloService", targetNamespace = "http://ws.itcast.cn/") @ XmlSeeAlso ({}) public interface HelloService {/***** @ param arg0 * @ return * returns java. lang. string */@ WebMethod @ WebResult (targetNamespace = "") @ RequestWrapper (localName = "sayHello", targetNamespace = "http://ws.itcast.cn/", className = "cn. itcast. ws. client. sayHello ") @ ResponseWrapper (localName =" sayHelloResponse ", targetNamespace =" http://ws.itcast.cn/", className =" cn. itcast. ws. client. sayHelloResponse ") public String sayHello (@ WebParam (name =" arg0 ", targetNamespace =" ") String arg0 );}

Import java.net. malformedURLException; import java.net. URL; import javax. xml. namespace. QName; import javax. xml. ws. service; import cn. itcast. ws. wsimport. helloService;/*** call Webservice through client programming **/public class App {public static void main (String [] args) throws Exception {URL wsdlUrl = new URL ("http: // 192.168.1.100: 6789/hello? Wsdl "); Service s = Service. create (wsdlUrl, new QName ("http://ws.itcast.cn/", "HelloServiceService"); HelloService hs = s. getPort (new QName ("http://ws.itcast.cn/", "HelloServicePort"), HelloService. class); String ret = hs. sayHello ("zhangsan"); System. out. println (ret );}}

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.