Web Service Simple Getting Started sample

Source: Internet
Author: User

Web Service Simple Getting Started sample
We typically implement Web service in a number of ways. I mainly use the CXF Apache plug-in and Axis 22.



Web Service is an online services solution provided by the application provider in order to solve each problem, it mainly uses the soap (simple Object Access Protocol) protocol, the transmission data format uses the XML format to describe the narration. Therefore, it also has cross-platform features.

Technologies that are widely used in the Web:

    1. TCP/IP: Common network protocol. Used by various devices
    2. HTML (an application under standard Universal Markup Language): Common user interface, able to display data using HTML tags
    3. Java: Write a common programming language that can be executed on any system, because Java has cross-platform features
    4. XML (a subset of standard common markup languages): a common data expression language, an easy way to transfer structured data over the web
They are characterized by their openness. Cross-platform, openness is the foundation of Web services.



Here is a simple getting started instance of Web service implementation using CXF Apache plug-ins


1======== Creating a new service interface

Package Com.clark;

Import Javax.jws.WebParam;
Import Javax.jws.WebService;

@WebService
Public interface Ihelloworld {

public string SayHello (@WebParam (name= "name") string name);

public int plus (int a,int b);
}
2======== Service Interface Implementation class

Package Com.clark.impl;

Import Com.clark.IHelloWorld;

public class Helloworldimpl implements Ihelloworld {

@Override
public string SayHello (string name) {
Return "Hello wolrd," +name;
}

@Override
public int plus (int a, int b) {
return a+b;
}

}

3============ Service Side

Package com.clark.service;

Import Javax.xml.ws.Endpoint;

Import Com.clark.impl.HelloWorldImpl;

public class Webserviceapp {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Web service Start");
Helloworldimpl implementor = new Helloworldimpl ();
String address = "Http://localhost:8080/IHelloWorld";
Endpoint.publish (address, implementor);
SYSTEM.OUT.PRINTLN ("Web service Started");
}
}

4============client (the following are primarily for Java common programs)
Package com.clark.client;

Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

Import Com.clark.IHelloWorld;

public class Helloworldclient {
public static void Main (string[] args) {
Jaxwsproxyfactorybean svr = new Jaxwsproxyfactorybean ();
Svr.setserviceclass (Ihelloworld.class);
Svr.setaddress ("Http://localhost:8080/CXFWebService/service/IHelloWorld");
Ihelloworld HW = (ihelloworld) svr.create ();
String name = Hw.sayhello ("CXF Apache implements Web Service");
int result = Hw.plus (2, 3);
SYSTEM.OUT.PRINTLN (name);
SYSTEM.OUT.PRINTLN (result);
}
}

4==============client (Web Development for integrated Web service in spring)
Package com.clark.web;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import Com.clark.IHelloWorld;

public class Helloworldclient {
public static void Main (string[] args) {
System.out.println ("Web Service start .....");
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");

Ihelloworld HelloWorld = (ihelloworld) Context.getbean ("Client");

String name = Helloworld.sayhello ("1111111111");
int result = Helloworld.plus (3, 4);
System.out.println (name+ "" +result);

System.out.println ("Web Service end .....");
}
}

Applicationcontext.xml Configuration for 5============spring
<?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:jaxws= "Http://cxf.apache.org/jaxws"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">

<jaxws:endpoint
Id= "HelloWorld"
Implementor= "Com.clark.impl.HelloWorldImpl"
address= "/ihelloworld"/>

<bean id= "Client" class= "Com.clark.IHelloWorld"
Factory-bean= "Clientfactory" factory-method= "create"/>

<bean id= "Clientfactory" class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
<property name= "ServiceClass" value= "Com.clark.IHelloWorld"/>
<property name= "Address"

Value= "Http://localhost:8080/CXFWebService/service/IHelloWorld"/>
</bean>
</beans>

Integrated Web Service Service (configuration of CXF servlet) in 6=============spring. xml
<?

XML version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 ">
<display-name>CXFWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<!--<param-value>classpath:applicationContext.xml</param-value>--
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-name>
<servlet-class>
Org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>

7============= start service, Address bar input
Http://localhost:8080/CXFWebService/service/IHelloWorld?

WSDL can see the corresponding SOAP protocol in the specification OK


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Web Service Simple Getting Started sample

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.