Basic configuration example of Java-Spring-WebService

Source: Internet
Author: User

Basic configuration example of Java-Spring-WebService
A long time ago, I initially learned about WebService, And I still felt "good.
When using Web Services, it feels like a common API, compared with an HTTP interface.
A major limitation of WebService is transactions. distributed transactions have increased a lot and have not been involved yet.

The two complete projects in the last year all have WebService configurations, but they are all configured by others.

Others, after all, are others.

As a programmer who loves to learn and apply what he has learned, I have to configure it myself ~

In the following example, I configured it step by step today and it succeeded.

Http://download.csdn.net/detail/fansunion/9218657 (CSDN)
(I plan to further improve the example on this basis in the future)

1. WebServiceClient interface project
If you want to use Web services for other projects, you just need to call the interface directly without paying attention to the Service implementation.

@WebServicepublic interface UserFacade {String query();}




Interface. Querying a string indicates that the Web Service is successfully called.

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) {// initialize 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 like 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
 
 
  
  
   
    
   
  
 




The key is jaxws: client id = remoteUserFacade, which is the core configuration of WebService.
In addition, pay attention to the xsiand xmlnsconfigurations of http://cxf.apache.org/jaxws. if no, an error should be reported.

Pom. xml configuration
 
  
   org.springframework
  spring-webmvc
  
   ${spring.version}
  
 
 
  
   org.aspectj
  aspectjweaver
  
   ${aspectj.version}
  
 
 
  
   org.aspectj
  aspectjrt
  
   ${aspectj.version}
  
 
 
  
   org.apache.cxf
  cxf-rt-frontend-jaxws
  
   ${cxf.version}
  
 
 
  
   org.apache.cxf
  cxf-rt-transports-http
  
   ${cxf.version}
  
 



II. Implementation Project of WebServiceImpl Interface
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;}}



Compared with common Java interface implementation classes, WebService annotations are added.
It seems that endpointInterface and serviceName are critical.
Currently, it is not clear whether these two attributes are required. Based on my existing experience, it is optional. If you do not write them, the default names and classes will be used according to certain rules.
I just thought it was wrong.
EndpointInterface is mandatory. Of course, if you do not write the endpointInterface, the program can be completely analyzed because there is "implements ".
If you are interested, Baidu.




UserFacdeImpl needs to be published as Web, and then configure the web. xml file.
The Spring configuration file is basically understood. It is mainly configured with the CXFServlet of WebService.
 
   
      
   
    contextConfigLocation
       
   
              classpath:spring-webservice.xml        
     
     
      
   
    org.springframework.web.context.ContextLoaderListener
     
    
      
   
    cxf
       
   
    org.apache.cxf.transport.servlet.CXFServlet
       
   
    1
     
    
      
   
    cxf
       
   
    /webService/*
     
   
 






Spring-webservice.xml
 
 
  
   
    
    
    
    
     
    
   
  
 


Introduce xml configuration of CXF and configure bean. The most important thing is "jaxws: server", which corresponds to "jaxws: Client" in the client project ~




Pom. xml
Completely consistent with the previous ~




Iii. Brief Review
UserFacadeClient project: an interface named 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 directly with the Client interface project ~


Iv. Testing and running process
1. Start the Web Project UserFacadeImpl
22:39:02. 195: INFO: oejs. Server: jetty-8.1.14.v20131031
22:39:02. 562: INFO:/: No Spring WebApplicationInitializer types detected on classpath
22:39:03. 087: INFO:/: Initializing Spring root WebApplicationContext
October 27 10:39:03 org. springframework. web. context. ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
October 27 10:39:03 org. springframework. web. context. support. XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Oct 27 22:39:03 CST 2015]; root of context hierarchy
October 27 10:39:03 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
October 27 10:39:03 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf. xml]
October 27 10:39:03 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 10:39:03 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-servlet.xml]
October 27 10:39:04 org. apache. cxf. service. factory. ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://impl.webservice.fansunion.cn/?userfacade from class cn. fansunion. webservice. UserFacade
October 27 10:39:04 org. apache. cxf. bus. spring. OldSpringSupport logWarning
WARNING: Import of META-INF/cxf/cxf-extension-soap.xml has been deprecated and is unnecessary.
October 27 10:39:04 org. apache. cxf. endpoint. ServerImpl initDestination
INFO: Setting the server's publish address to be/UserFacade
October 27 10:39:04 org. springframework. web. context. ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1644 MS
22:39:04. 839: INFO: oejs. abstractactctor: Started [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 the Java application WebServiceTest.
October 27 10:39:08 org. springframework. context. support. ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org. springframework. context. support. ClassPathXmlApplicationContext @ 2484e723: startup date [Tue Oct 27 22:39:08 CST 2015]; root of context hierarchy
October 27 10:39:08 org. springframework. beans. factory. xml. XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
October 27 10:39:09 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 through a browser? A URL similar to wsdl directly displays the returned results.
It seems that it is not feasible. Study it later ~


V. Final description
1. For the sake of simplicity, these two projects are very simple and there is no code or configuration irrelevant to WebService.
The sparrow is small and dirty
2. For the sake of simplicity, the server uses localhost and writes it to death, you know ~
3. Viewing the startup logs of services and applications is very valuable ~


This time, let's summarize it ~

 

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.