Learning essays-Java WebService,-javawebservice
WebService can convert applications into network applications. It is a simple framework for sending and receiving messages that can be operated together.
The basic webService platforms are XML and HTTP.
HTTP is the most common Internet protocol;
XML is the foundation of webService, because it can be used between different platforms and programming languages.
WebService platform elements:
SOAP (Simple Object Access Protocol );
UDDI (General description, discovery, integration );
WSDL (web service Description Language );
WebService has two types of applications
1) reusable application components: some features are often used by different applications, such as weather forecast. WebService can provide application components as services;
2) connecting to existing software: by providing different programs with a way to link their data, the problem of collaborative office is solved.
Simple Program:
Create a web service project for a service:
Package com. service;
Import javax. jws. WebMethod;
Import javax. jws. WebService;
Import javax. xml. ws. Endpoint;
@ WebService // mark the class as webService
Public class Test {
@ WebMethod // annotation of the method called by the client
Public String getString (String name ){
Return name. toUpperCase ();
}
Public static void main (String [] args ){
// Publish to server parameter 1: network protocol + ip + port number (cannot be occupied) + context root + call method parameter 2: new instance of this class
Endpoint. publish ("http: /localhost: 8085/Test", new Test ());
// Indicates that the compilation is successful but not written.
System. out. println ("OK ");
}
}
// The configuration file is not clear yet. If the test address is not configured, no response is returned.
Configure an xml file
<? 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: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "The http://www.springframework.org/schema/aop" xmlns: tx = "The http://www.springframework.org/schema/tx"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.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://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc.xsd>
<! -- Register the component scanner -->
<Context: component-scan base-package = "com. service"/>
</Beans>
Test URL: http: // localhost: 8085/Test? Wsdl
Use the webService soapUI 4.5.2 software link http: // localhost: 8085/service/Test to automatically generate the Application service
<Soapenv: Envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: ser = "http://service.com/">
<Soapenv: Header/>
<Soapenv: Body>
<Ser: getString>
<! -- Optional: -->
<Arg0> yyyy </arg0> // <arg0>? </Arg0> what is the original display? Required Parameter
</Ser: getString>
</Soapenv: Body>
</Soapenv: Envelope>
<S: Envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/">
<S: Body>
<NS2.: getStringResponse xmlns: NS2. = "http://service.com/">
<Return> YYYY </return> // <return>? </Return> what is the original display? For processing results
</Ns2: getStringResponse>
</S: Body>
</S: Envelope>
2. Run the generate command in the Command Prompt window.
First, create a web service project for the client.
Open the command window and enter
Format:Wsimport-s "src directory"-p "generate class package name"-keep "wsdl publishing address"
1) The "src directory" address cannot contain spaces
2) do not miss "? Wsdl"
Effect:
Wsimport-s E: \ WorkSpace \ maven \ client \ src-p com. client-keep http: // localhost: 8085/Test? Wsdl
Parsing WSDL...
Generating code...
Compiling code...
Refresh the project to view the generated client code.
However, the client code has garbled characters, which have not been resolved yet.