SPRING3.0+CXF WebService Development __spring

Source: Internet
Author: User
Tags tomcat server wsdl

For webservice, just a little bit of study before, is a smattering of it. Today, the project needs to do such a function, external supply interface, I pretended to know webservice like, said no problem, small things. In fact, I want to wait for the weekend to check their own data, learn, because this thing has been done before, it is indeed not difficult. Who knows the weekend something delayed, Monday came to work. After a while, I suddenly remembered that there might have been some relevant examples in the project, because the project platform had already been built. As expected, so much simpler. Here is an example to post, and then to explain the small problems encountered in the process.

A simple example is divided into three steps (basic environment, WebService class development, configuration file)

Basic Environment: First spring3.0 environment to build, CXF related jar introduced.

WebService class development (interface and implementation classes):

Package com.cxf.server;

Import java.util.Date;
Import java.util.List;

Import Javax.jws.WebService;

Import Com.vo.WorkUnitVO;

@WebService Public
interface Iworkunitinfo {public

	list<workunitvo> getworkunitbydate (date date);
}
Package com.cxf.server;

Import java.util.Date;
Import java.util.List;

Import Javax.jws.WebService;

Import org.springframework.beans.factory.annotation.Autowired;

Import Com.service.IWorkUnitService;
Import com.. Vo. Workunitvo;

@WebService (endpointinterface = "com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo") Public
class Workunitinfoimpl implements Iworkunitinfo {

	@Autowired the
	other classes in the private Iworkunitservice workunitservice;//project, Gets the result set used.
	
	@Override public
	list<workunitvo> getworkunitbydate (date date) {
		return Workunitservice.getworkunitbydate (date);

	Public Iworkunitservice Getworkunitservice () {return
		workunitservice;
	}

	public void Setworkunitservice (Iworkunitservice workunitservice) {
		this.workunitservice = Workunitservice;
	}
	
}

Configuration file 1: (web.xml configuration file joins CXF related profile)

<!--CXF Web Services configuration information-->
 <servlet>
  <display-name>cxf servlet</display-name>
  <servlet-name>CXFServlet</servlet-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>/services/*</url-pattern><!--Note the configuration/services/here is part of generating a WSDL path The path to the WSDL: http://ip:8080/project path/services/spring cxf configuration file address?wsdl-->
 </servlet-mapping>
Configuration file 2: (Spring cxf configuration file)

<?xml version= "1.0" encoding= "UTF-8"?> <!--START Snippet:beans--> <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-extension-soap.xml "/> <import resource=" Classpath:meta-inf/cxf/cxf-servlet.xml "/> <jaxws: Endpoint id= "HelloWorld" implementor= Com.cslc.crius.common.ws.cxf.server.HelloWorldImpl "address="/helloworld "/> <jaxws:endpoint id=" Workunitinfo "implementor=" #workUnitInfoImpl "address="/workunitinfo "/>" ; Bean id= "Workunitinfoimpl" class= "Com.cslc.crius.common.ws.cxf.server.WorkUnitInfoImpl" > &Nbsp;<!--If there is a need to load other classes in the WebService, it is not possible to use annotations, you need to display the declaration in the configuration file-->   <property name= "Workunitservice" ref= " Workunitserviceimpl "></property> </bean> </beans> <!--end Snippet:beans-->
After the completion of these steps, you can perform the relevant tests, publish to the Tomcat server, and start the server.
The first step is to test the WSDL:
Access the ADDRESS?WSDL in the http://ip:8080/project path/services/spring cxf configuration file, for example: http://129.0.0.0:8080/abc/services/WorkUnitInfo?wsdl

Step Two: Test WebService
1. WebService client Spring configuration file Wsclient-context.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!--START Snippet:beans--> <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/schema/
		Jaxws.xsd "> <bean id=" workunitinfoclient "class=" Com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo " Factory-bean= "Workunitinfofactory" factory-method= "create" > </bean> <bean id= "Workunitinfofactory" class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name= "ServiceClass" Com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo "/> <property name=" Address "value=" http://localhost:8080/ VIMINAL/SERVICES/WORKUNITINFO?WSDL "></property> </bean> </beans> <!--end Snippet:beans--& Gt

2, call the WebService interface test program

package com.cxf.client;
Import Java.util.Date;

Import java.util.List;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.cslc.crius.common.utils.DateUtils;
Import Com.cslc.crius.common.ws.cxf.server.IWorkUnitInfo;

Import Com.cslc.crius.viminal.vo.WorkUnitVO;
        Public final class Client {private Client () {} public static void Main (String args[]) throws Exception { START snippet:client Classpathxmlapplicationcontext context = new Classpathxmlapplicationc

        Ontext (new string[] {"Client/wsclient-context.xml"});
        Iworkunitinfo work = (iworkunitinfo) context.getbean ("Workunitinfoclient");
        Date date = Dateutils.parsedate ("2012-10-01");
        list<workunitvo> r = work.getworkunitbydate (date);
        for (int i=0;i<r.size (); i++) {System.out.println (R.get (i) getworkunittitle ());
        } system.exit (0); End Snippet:client}} 
The basic step is above, the interface provided is the URL used to test the WSDL above.

<pre>

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.