使用cxf+javax开发WebService接口及调用

1、jar包:cxf-2.5.3.jar   neethi-3.0.2.jar    wsdl4j-1.6.2.jar    xmlschema-core-2.0.2.jar

2、配置文件:<---cxf.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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-3.1.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" />
            
	<bean id="getServiceImpl" class="com.netmarch.webservice.getServiceImpl" />
	<jaxws:endpoint id="getService" implementor="#getServiceImpl" address="/getService" />  
		
</beans>

	<---applicationContex.xml--->
......
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    	classpath*:applicationContext.xml
   	classpath*:cxf.xml
   	</param-value>
</context-param>
......
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webService/*</url-pattern>
	</servlet-mapping> 
......

<---getService.java--->

package com.netmarch.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(targetNamespace = "http://jdk.study.hermit.org/client")
public interface getService {	
	@WebMethod(operationName="setProject")
	@WebResult(name = "result")
	public String setProject(@WebParam(name = "json") String json);//项目
	
	@WebMethod(operationName="setZl")
	@WebResult(name = "result")
	public String setZl(@WebParam(name = "json") String json);//专利接口
		
}

接口的实现

package com.netmarch.webservice;

public class getServiceImpl implements getService {	
	//接口的实现	
	......

}

<---testWebService.java--->

package com.netmarch.webservice;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class text {
	
    public static void main(String[] args) {
	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
        //注册WebService接口    
        factory.setServiceClass(getService.class);    
	factory.setAddress("http://localhost:8083/webService/getService");    
        getService getService = (getService)factory.create();    
        System.out.println("开始调用webservice..."); 
        
        String json = "{...}";

        System.out.println("返回的信息是:"+getService.setZl(json));
    }
}

项目启动后,运行main(),即可调用service接口。

猜你喜欢

转载自blog.csdn.net/qq_35414397/article/details/82864085
今日推荐