Java calls WebService instances

Source: Internet
Author: User
Tags wsdl

WebService is a service-specific architecture that provides services through a standard web protocol to ensure that application services from different platforms can interoperate. At present, the most popular implementation is the use of. NET and JAVA Two language technology, and the two implementation languages can interoperate with each other;

The

Below is mainly about language ; >w Ebservice definition and invocation, through java language development xfire , cxf jws way to achieve has its own characteristics, because I use Cxf way more skilled, and, CFX and s pring seamlessly combined, so the following highlights the cfx Span style= "font-family: the song Body;" The implementation of the method .

Since the Webservice implementation is divided into two development modes: Server and client, the next part introduces WebService Server Development, client development and instance testing.

    1. Environment preparation

JDK1.6 and above, the required jar files are:commons-logging-1.1.1.jar, Cxf-2.4.3.jar, Neethi-3.0.1.jar, Spring-aop-3.0.5.release.jar, Spring-asm-3.0.5.release.jar, Spring-beans-3.0.5.release.jar, Spring-context-3.0.5.release.jar, Spring-context-support-3.0.5.release.jar, Spring-core-3.0.5.release.jar, Spring-expression-3.0.5.release.jar, Spring-web-3.0.5.release.jar, Wsdl4j-1.6.2.jar, Xmlschema-core-2.0.1.jar.

1. Server-Side development

Custom interface Classes

Package cn.test.ws.service;

import Javax.jws.WebParam;

import Javax.jws.WebService;

@WebService

Public Interface Greetingservice {

Public String Welcome (@WebParam (name= "username") string username);

Public int getnum (@WebParam (name= "number") int number);

}

Defining an interface implementation class

Package Cn.test.ws.service.impl;

import Cn.test.ws.service.GreetingService;

Public class Greetingserviceimpl implements Greetingservice {

@Override

Public String welcome (string username) {

System. out. println (" Server-side method:greeting ()");

return "Hell0" +username+ ", welcome you to call JAVA under the WebService service !";

}

@Override

Public int getnum (int number) {

System. out. println (" Server-side method:getnum ()");

return number;

}

}

Spring.xml Configuration

<?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 ">

<import resource="Classpath:meta-inf/cxf/cxf.xml" />

<import resource="Classpath:meta-inf/cxf/cxf-servlet.xml" />

<jaxws:server id="Greetingservice"

serviceclass="Cn.test.ws.service.GreetingService"

address="/greetingservice">

<jaxws:serviceBean>

<bean class="Cn.test.ws.service.impl.GreetingServiceImpl" />

</jaxws:serviceBean>

<jaxws:features>

<bean class="org.apache.cxf.feature.LoggingFeature" />

</jaxws:features>

</jaxws:server>

</beans>

2. Client Development

Public Static void Main (string[] args) {

jaxwsdynamicclientfactory DCF = jaxwsdynamicclientfactory. newinstance ();

String Wsurl = "http://127.0.0.1:8080/WS/GreetingService?wsdl";

String method = "Welcome";

String method2 = "Getnum";

Try {

Client client = dcf.createclient (Wsurl);

Object[] Objects =client.invoke (method, "TEST");

Object[] Numbers =client.invoke (method2,6);

System. out. println (Objects[0].tostring ());

System. out. println (Numbers[0]);

} catch (Exception e) {

E.printstacktrace ();

}

}

3. Example Test

The webservice test can be tested using the client main method described above, as well as by testing in a browser, and by means of a dedicated WebService test Tool .

Enter Http://IP: port / project name/greetingservice?wsdl in the browser to see the published service-side information, and to display the information returned by the server by entering the parameters, such as: Http://IP: Port / project name/greetingservice/welcome?username=test.

Java calls WebService instances

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.