Java-spring-webservice most basic Configuration example

Source: Internet
Author: User
Tags soap webservice annotation wsdl

Very early, very early, on the preliminary study of WebService, the feeling is still relatively "good".
With Web services, it feels like a normal API, compared to an HTTP interface.
WebService has a big limitation, that is, the problem of distributed transactions has risen a lot, not yet.

The last 1 years to do 2 more complete projects, have WebService configuration, but, are configured by others.

Others, after all, is someone else's.

As a programmer who loves to learn and apply my knowledge, I will also make a configuration for myself.

The following example, I personally step by step today, and run successfully.

csdn:http://download.csdn.net/detail/fansunion/9218657
(I intend to further refine the example on this basis.)

First, WebServiceClient interface project
Other projects if you want to use a Web service, call the interface directly, there is no need to focus on the implementation of the service.

@WebServicepublic interface Userfacade {String query ();}




Interface, has been the most minimalist. Querying a string indicates that the Web service invocation succeeded.

Package Cn.fansunion.webservice.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Cn.fansunion.webservice.UserFacade; public class WebServiceTest {public static void main (string[] args) {//Initializes the spring context, The WebService file is located in the SRC directory (or classpath) applicationcontext ac = new Classpathxmlapplicationcontext ("classpath:/ Spring-webservice.xml ");//Get bean by ID, I feel this" jaxws:client id= "Remoteuserfacade" " The combination of WebService syntax and spring syntax Userfacade userfacade= (userfacade) Ac.getbean ("Remoteuserfacade");//query, return and print the string " WebService "System.out.println (Userfacade.query ());}}




Spring-webservice.xml
<?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= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:jaxws= "Http://cxf.apache.org/jaxws" xmlns:soap= "http ://cxf.apache.org/bindings/soap "xmlns:util=" Http://www.springframework.org/schema/util "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 Cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd Http://cxf.apache.org/jaxws Http://cxf.apache.org/schemas/jaxws.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org /schema/aop/spring-aop.xsd Http://www.springframework.org/schema/util Http://www.springframewOrk.org/schema/util/spring-util-3.0.xsd "><!--enable Autowire--><context:annotation-config/>< Context:component-scan base-package= "Com.fansunion.webservice"/><jaxws:client id= "Remoteuserfacade" address = "Http://localhost:8080/webService/UserFacade" serviceclass= "Cn.fansunion.webservice.UserFacade"/></beans >




The key is Jaxws:client id= "Remoteuserfacade", this is the core configuration of WebService.
In addition, you need to pay attention to the Http://cxf.apache.org/jaxws xsi and xmlns configuration, if not, should be an error.

Pom.xml Configuration
<dependency><groupid>org.springframework</groupid><artifactid>spring-webmvc</ Artifactid><version>${spring.version}</version></dependency><dependency><groupid >org.aspectj</groupid><artifactid>aspectjweaver</artifactid><version>${ Aspectj.version}</version></dependency><dependency><groupid>org.aspectj</groupid ><artifactid>aspectjrt</artifactid><version>${aspectj.version}</version></ Dependency><dependency><groupid>org.apache.cxf</groupid><artifactid> cxf-rt-frontend-jaxws</artifactid><version>${cxf.version}</version></dependency>< dependency><groupid>org.apache.cxf</groupid><artifactid>cxf-rt-transports-http</ Artifactid><version>${cxf.version}</version></dependency></dependencies>



Second, the implementation of the Webserviceimpl interface project
Package Cn.fansunion.webservice.impl;import Javax.jws.webservice;import Org.springframework.stereotype.Service; Import Cn.fansunion.webservice.UserFacade; @WebService (endpointinterface = "Cn.fansunion.webservice.UserFacade", ServiceName = "Userfacade") @Service ("Userfacadeimpl") public class Userfacdeimpl implements Userfacade {@ Overridepublic String Query () {return ' WebService ';}}



The WebService annotation is more than the normal Java interface implementation class.
It seems that endpointinterface and servicename are critical.
It is not clear whether these 2 properties are necessary, according to my own experience, is optional, if not written, will follow a certain rule with the default name and class.
Just finished writing, I feel wrong.
Endpointinterface is supposed to be required, of course, if not written, the program can be completely analyzed, because there is "implements".
Interested in, own Baidu.




The Userfacdeimpl needs to be published as a web, followed by the CONFIG. xml file.
Spring configuration file, basically understand, mainly is configured WebService Cxfservlet.
<?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 "version=" 2.5 "> <context-param> <param-name> contextconfiglocation</param-name> <param-value> Classpath:spring-webservice.xml </param -value> </context-param> <listener> <listener-class>    Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> & Lt;servlet-name>cxf</servlet-name> <url-pattern>/webService/*</url-pattern> </ Servlet-mapping> </web-app> 






Spring-webservice.xml
<!--Import CXF--><import resource= "Classpath:meta-inf/cxf/cxf.xml"/><import resource= "classpath: Meta-inf/cxf/cxf-extension-soap.xml "/><import resource=" Classpath:meta-inf/cxf/cxf-servlet.xml "/>< Bean id= "Userfacade" class= "Cn.fansunion.webservice.impl.UserFacdeImpl" ></bean><jaxws:server id= " Webserviceuserfacade "servicebean=" #userFacade "address="/userfacade "></jaxws:server><!--enable Autowire- -><context:annotation-config/><context:component-scan base-package= "Com.fansunion.webservice"/>


Introduce the XML configuration of CXF, configure the Bean, the most important or "jaxws:server", and the client project "Jaxws:client" corresponds to it ~




Pom.xml
Exactly the same as before




Iii. A brief review of the
Userfacadeclient Project: Just one interface Userfacade
Userfacadeimpl Project: interface Implementation Userfacadeimpl, Spring-webservice.xml, Pom.xml, Web. xml
Test items: WebServiceTest, Pom.xml, Spring-webservice.xml
For convenience, we put the test project together directly with the Client interface project ~


Iv. Testing and operating processes
1. Start Userfacadeimpl this web project
2015-10-27 22:39:02.195:info:oejs. server:jetty-8.1.14.v20131031
2015-10-27 22:39:02.562:info:/:no Spring Webapplicationinitializer types detected on Classpath
2015-10-27 22:39:03.087:info:/:initializing Spring Root Webapplicationcontext
October 27, 2015 10:39:03 pm Org.springframework.web.context.ContextLoader initwebapplicationcontext
Info:root Webapplicationcontext:initialization started
October 27, 2015 10:39:03 pm Org.springframework.web.context.support.XmlWebApplicationContext Preparerefresh
info:refreshing Root webapplicationcontext:startup Date [Tue Oct 22:39:03 CST 2015]; Root of context Hierarchy
October 27, 2015 10:39:03 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Spring-webservice.xml]
October 27, 2015 10:39:03 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Meta-inf/cxf/cxf.xml]
October 27, 2015 10:39:03 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Meta-inf/cxf/cxf-extension-soap.xml]
October 27, 2015 10:39:03 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Meta-inf/cxf/cxf-servlet.xml]
October 27, 2015 10:39:04 pm Org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildservicefromclass
Info:creating Service {Http://impl.webservice.fansunion.cn/}userfacade from class Cn.fansunion.webservice.UserFacade
October 27, 2015 10:39:04 pm Org.apache.cxf.bus.spring.OldSpringSupport logwarning
Warning:import of Meta-inf/cxf/cxf-extension-soap.xml have been deprecated and is unnecessary.
October 27, 2015 10:39:04 pm Org.apache.cxf.endpoint.ServerImpl initdestination
Info:setting the server ' s publish address to Be/userfacade
October 27, 2015 10:39:04 pm Org.springframework.web.context.ContextLoader initwebapplicationcontext
Info:root Webapplicationcontext:initialization completed in 1644 MS
2015-10-27 22:39:04.839:info:oejs. abstractconnector:started Se[email protected]:8080
------------------------------------------------
Jetty startup finished in 3.1 s.
Used memory:5.5 MB of 121.8 MB (1.8 GB maximum)
Console available:type "Help".
------------------------------------------------


2. Run WebServiceTest this Java application
October 27, 2015 10:39:08 pm Org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh
info:refreshing org[email protected]2484e723:startup Date [Tue Oct 22:39:08 CST 2015]; Root of context Hierarchy
October 27, 2015 10:39:08 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Spring-webservice.xml]
October 27, 2015 10:39:09 pm Org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildservicefromclass
Info:creating Service {Http://webservice.fansunion.cn/}userfacadeservice from class Cn.fansunion.webservice.UserFacade
WebService


3. You can also access http://localhost:8080/webService/UserFacade/query?wsdl
View the definition of this webservice


I want to access http://localhost:8080/webService/UserFacade/query?wsdl similar URLs through a browser, directly displaying the returned results.
It seems not very good, oh, back to study it ~


V. Final description
1. For the sake of simplicity, these 2 projects are very simple, with no trivial code and configuration at all with WebService.
It's a perfectly formed, huh?
2. For the sake of simplicity, the server uses localhost, and it's written dead, you know ~
3. See the service and application start-up log, is very valuable ~


This is the first to summarize here ~


Vi. Source Code
http://download.csdn.net/detail/fansunion/9218657


Ray fansunion-Programmer A
October 27, 2015
Hubei-Wuhan-courtesy Gate
qq:240370818
: fansunion

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-spring-webservice most basic Configuration example

Related Article

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.