WebService Study Record

Source: Internet
Author: User
Tags soap wsdl

-------------------------------------------PS: This WebService service must be open all the time, it can't be accessed by closing.
Web Service Tutorials

I. Introduction of WebService

1. Usage Scenarios

-Different mobile Client access
-Access to third-party projects

2. Ways to access third-party apps
ISO seven-layer model: Physical layer, Data link layer, network layer, Transport layer, presentation layer, Session layer, application layer
-Socket access: socket belongs to the transport layer, it is the implementation of TCP/IP protocol, including TCP/UDP, it is all communication protocol
Base, the HTTP protocol requires socket support, with sockets as the basis
Socket Communication Features:
1. Open the port, the communication is long-connected communication, very easy to be blocked by the firewall, through the heartbeat mechanism
To achieve, develop the difficulty fragment
2. Transmission of data is generally a string, not strong readability
Lj|16|1|60|up
3. Socket port is not easy to promote
http:17.23.23.2:2345 www.jd.com www.360buy.com
4. Performance relative to other communication protocols is optimal
-HTTP protocol access: a protocol that belongs to the application layer, encapsulating the socket
1. Cross-platform
2. Data transfer is not friendly:
GET Request: http://127.0.0.1:8888?username=lj&pwd=1234
3. Services provided to third-party applications wishing to expose the service interface externally

Problem:
1. Data encapsulation is not friendly: data can be encapsulated in XML
2. Services (HTTP + XML) that want to provide Web access to third-party applications = Web Service

-Webservice
1.webService Introduction

Rules for 2.webservice
-XML
-Soap: (Simple Object Access Protocol): Basic Objects Access Protocol
-Wsdl:webservice Description Language, which is also implemented by XML
Advantages of 3.webservice

4.webservice Professional Name
1. xml
2. wsdl
3. Soap

Second, call the third-party WS service

1. Experience WebService www.webxml.com.cn

2. Prepare the client, call the third party's WebService service, let the project have the function of mobile phone check number
-General access Mode HttpClient
1. Http-get//soap 1.2 without support low version so one with Soap 1.1
2. Http-post
3. Soap (Http+xml)
Question: 1. How to parse results


2. How to pass object parameters

-Recommended ways to access
Support for WebService with JAX-WS package after JDK 1.6 release
-The way to declare webservice by annotations
-Publish webserive service via JDK Endpoint.publish ()

Earlier versions: JAX-RPC (remote produce call)

WebService is included in the specification, and other platforms support the specification: jee\php\.net
Support Wsimport mode: Generate local agent for remote webservice, and then through local agent
To access webservice!!!!!!!
-The location of the Wsimport command:
C:\Program Files\java\jdk1.7.0_04\bin
Requirements
1. JDK version is 1.6.21 and above JDK
2. The JDK version installed by the operating system is consistent with MYECLISPE and the default specified version

-Wsimport Use:
Remember to set the JDK\BIN environment variable to specify path
Grammar Wsimport [opations] <wsdl_uri>
-WSDL_URI:WSDL's Uniform Resource Identifier
-D: Specifies the location of the file to be output
-S: To parse the Java source code, the default parsing is a class bytecode
-P: Specifies the package name of the output
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------

1. Call WebService: both Php,.net and Java use local proxies

2. Java is using the

Wsimport-s./-P CN.IT.WS.C Http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL gets the service and then calls

-----------------------------------------------------------Release WebService-------------------------------------------------- --------------------------------------

Ps:webservice Yes release is one of the Java JDK, one of SE

PS: If you specify an error multiple times, you should add comments like this

Package bee.test;

Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
Import Javax.xml.ws.Endpoint;

Import Bee.model.Phone;
@WebService (servicename= "Phonemanager",//Modify service Name
Targetnamespace= "Http://bee.test")//Modify namespace, default package name, reverse
public class Phoneservice {
@WebMethod (operationname= "Getmobileinfo")
Public @WebResult (name= "phone") Phone Getphoneinfo (@WebParam (name= "Osname") String osname) {
Phone phone=new phone ();
if (Osname.endswith ("Android")) {
Phone.setname ("Android");p Hone.setowner ("Google");p hone.settotal (80);
}else if (Osname.endswith ("ios")) {
Phone.setname ("ios");p Hone.setowner ("Apple");p Hone.settotal (15);
}else{
Phone.setname ("Windows Phone");p Hone.setowner ("Microsoft");p Hone.settotal (5);
}
return phone;
}

public static void Main (string[] args) {
String address1= "Http://127.0.0.1:8888/ws/phoneService";
Endpoint.publish (Address1, New Phoneservice ());
SYSTEM.OUT.PRINTLN ("WSDL Address:" +address1+ "?) WSDL ");
}
}



Three, customizing and publishing a WebService service
-declares that a business service declares a WebService service
through @webservice annotations
-Publish WebService service
Endpoint.publish () Publish
-Wsimport generate a local agent to access your published WebService
Wsimport


Iv. WebService Transport entity objects

Five , Exercise:


1. Calling the teacher machine's WS-Service
Step:
1. The system JDK environment is consistent with the MyEclipse JDK environment, 1.6.021 or more
2. Know the WDSL path of the server
http://192.16 8.114.10:8888/ws/phoneservice? WSDL
3. Command line wsimport-s./-P CN.ITCAST.WS.D http://192.168.114.10:8888/ws/phoneService? WSDL
4. Import the Java class parsed by Wsimport, and then write a main test
Moblieosinfoservice ws=new moblieosinfoservice ();
Moblieosinfo Phoneserviceport = Ws.getmoblieosinfoport ()
Phoneos phone=phoneserviceport.getmobileosinfo (" Android ");

2. Create your own WS service and publish, invoke
Step: 1. Declare the WebService service (@webService) 2. Released WebService (Endpoint.publish) 3. Client Access (Wsimport)
1. Create a Java Bean:phoneservice to be published externally via WebService
2. In the Java Portal The main method publishes a service endpoint via endpoint
String address1= "Http://192.168.19.10:8888/ws/phoneService";
Endpoint.publish (Address1, New Phoneservice ());
System.out.println ("Adrress1 's WSDL address" +address1+ "?) WSDL ");


Vi. description of the WSDL file: WebService Service Descriptions


Vii. Modify the WSDL file with annotation configuration to make it more readable
Demand:
1. To change the method input parameter name, return parameter name
2. To change the service name, method name
3. Some methods of the business class do not want to be released externally
-Only public-modified methods are published by default in WebService form
Private protected, default, will not be published
-Some public methods do not want to be released externally

Implementation: Annotation configuration modifies WSDL file

Focus:
1. WebService's role, rules and benefits
2. Professional nouns
WSDL, SOAP, XML
3. Wsimport Generate local agent
4. Declaration and release of WebService by Jee mode
-Statement: @webservice
-Release: EndPoint
5. wsdl File description
6. Improve readability by modifying annotations, configuring WSDL files

--------------------------------------------------------------------------------------------------------------- ----------------------------

Through the JDK
Disclaimer: @Webservice
Release: EndPoint
Insufficient: WebService service can be turned on when Tomcat is started, preferably with spring integration
wish to have a list of WebService services
Implemented through the WebService framework: Axis2, Xfire


Iv. Use the CXF framework to publish the WebService service and use client remote access WebService
1. CXF Introduction: The framework of SOA
* CXF is a combination of Celtrix (ESB framework) and XFire (Webserivice) and donated to Apache
* The core of the CXF is the Org.apache.cxf.Bus (bus), similar to the spring ApplicationContext
* CXF default is spring-dependent
* Apache CXF release jar, if all put in Lib, need to JDK1.6 and above, otherwise will report JAX-WS version inconsistency problem
* CXF built-in jetty server, which is a servlet container, like Tomcat
2.CXF Features
1. Seamlessly interfacing with spring and servlet, the CXF framework integrates the servlet container jetty
2. Support Annotations to publish WebService
3. Ability to display a list of WebService services
4. Ability to add interceptors: input interceptors, output interceptors:
Input the log information interceptor, output log interceptor, User Rights authentication Interceptor

---------------------------------------------WebService the XML at transfer time, RESTful is the transfer JSON---------------------------------------------------------------

PS: Play Jar Bag


Five, SPRING+CXF integration to manage WebService

/** released through the CXF framework WebService
* 1. Serverfactorybean
*-Do not set annotations can also be published WebService service, does not support annotations
*-Does not support the addition of interceptors
* 2. Jaxwsserverfactorybean often use this
*-Support annotations
*-Can add interceptors
* 3. WebService Access Process:
* 1. Detects if the WSDL described by the local agent is consistent with the WSDL on the service side, commonly known as handshake
* 2. Communication through the SOAP protocol, with post requests, and data encapsulated in XML that satisfies the SOAP specification
* 3. The return data is also in soap communication, and the data is encapsulated in XML that satisfies the SOAP specification
* @param args

Inbound Message
----------------------------
Id:5
address:http://127.0.0.1:9999/ws/cxf/languangeservice?wsdl
Http-method:get
Content-type:
Headers: {accept=[text/html, image/gif, Image/jpeg, *; q=.2, *//**; q=.2], connection=[keep-alive], content-type=[ NULL], host=[127.0.0.1:9999], user-agent=[java/1.7.0_51]}
--------------------------------------
July 17, 2017 7:34:25 pm Org.apache.cxf.interceptor.AbstractLoggingInterceptor log
information: Inbound message
----------------------------
Id:6
Address:http://127.0.0.1:9999/ws/cxf/languangeservice
Encoding:utf-8
Http-method:post
content-type:text/xml; Charset=utf-8
Headers: {accept=[text/xml, multipart/related], connection=[keep-alive], content-length=[208], content-type=[ Text/xml; Charset=utf-8], host=[127.0.0.1:9999], soapaction=[""], User-agent=[jax-ws RI 2.2.4-B01]}
Payload: <?xml version= "1.0"? ><s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/" > <s:body><ns2:getlanguage xmlns:ns2= "http://a.cxf.ws.it.cn/" ><position>1</position></ Ns2:getlanguage></s:body></s:envelope>
--------------------------------------
July 17, 2017 7:34:25 pm Org.apache.cxf.interceptor.AbstractLoggingInterceptor log
information: Outbound message
---------------------------
Id:6
Encoding:utf-8
Content-type:text/xml
Headers: {}
Payload: <soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" ><soap:Body>< Ns2:getlanguageresponse xmlns:ns2= "http://a.cxf.ws.it.cn/" ><LANGUAGE>JAVA</LANGUAGE></NS2: Getlanguageresponse></soap:body></soap:envelope>
--------------------------------------


Public static void Main (string[] args) {
languageservice languageservice=new Languageserviceimpl ();
Serverfactorybean bean=new Serverfactorybean ();
//endpoint: Address, Implementing Object
bean.setaddress ("Http://192.168.114.10:9999/ws/cxf/languangeService");
Bean.setserviceclass (Languageservice.class);//provide WEBSERVCIE business class or interface externally
Bean.setservicebean (Languageservice);//service Implementation Bean
bean.create ();//Create, publish WebService
System.out.println ("WSDL Address: Http://192.168.114.10:9999/ws/cxf/languangeService?WSDL");
}
*/
Public static void Main (string[] args) {
languageservice languageservice=new Languageserviceimpl ();
Jaxwsserverfactorybean bean=new Jaxwsserverfactorybean ();
//endpoint: Address, Implementing Object
bean.setaddress ("Http://127.0.0.1:9999/ws/cxf/languangeService");
Bean.setserviceclass (Languageservice.class);//provide WEBSERVCIE business class or interface externally
Bean.setservicebean (Languageservice);//service Implementation Bean
//Add input blocker: Enter the Interceptor that displays the log information
bean.getininterceptors (). Add (New Loggingininterceptor ());
//Add output blocker: Output an interceptor that displays log information
bean.getoutinterceptors (). Add (New Loggingoutinterceptor ());

bean.create ();//Create, publish WebService
System.out.println ("WSDL Address: Http://127.0.0.1:9999/ws/cxf/languangeService?WSDL");
}


Implementation steps:
1. Add Cxf.jar Package (Integrated Spring.jar, Servlet.jar), Spring.jar package, Servlet.jar package
2. Write the business class and publish it through CXF WebService
Employee Management:
Methods: Add employees, query employees

3. Add a Servlet for the CXF request to process the WebService request
Filter the Address/ws/*
4. Configure spring configuration file: Applicationcontext.xml, CXF Bean is configured in spring

5. Configure the CXF Servlet in Web. XML to add spring monitoring

6. Generate local agent via Wsimport, Access WebService

Vi. Exercises:
Spring and CXF Integration
-Add Cxf.jar, add Spring frame, Servlet.jar package
-Web. XML configuration
1. Configure the CXF servlet to handle all WebService requests
Configuring the Servlet
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<url-pattern>/ws/*</url-pattern>
2. Load the Spring listener
-Business class, WebService business beans released through CXF
Employee Management WebService Service
1. Add Employees
2. Check all employees
3. Save employees through List<employee>
-Spring configuration file Configuration Bean
Configure CXF
-Client Access
Test

1.web.xml

2. Just these two will do, and when Tomcat starts, the service will be on the cloth.

--------------------------------------------------------------------------------------------------------

Seven, WebService test tools
Web Service Exprlorer

Eight, soap1.1 and soap1.2 difference

-Soa: Service-oriented architecture, which is an idea
Service 1, Service2, service3 all services for Web, Plug and Play
IBM strongly advocates SOA architecture and wants to develop software in a way that assembles computers
-Components for various services (WebService): Hard drives, CPUs, memory strips
-Enterprise Service Bus: Motherboard

WebService Study Record

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.