在java web项目中集成webservice

公司要求在项目中加入webservice服务,由于项目中使用了spring框架,因此在这里使用与spring兼容性较好的cxf来实现java

cxf所需jar包web

在此输入图片描述

spring的jar包就不贴了spring

一:建立webservice服务器apache

1)建立一个服务接口浏览器

package com.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(@WebParam(name = "arg0") String text);
}

2)是接口实现类 package com.service.impl;服务器

import javax.jws.WebService;

import com.service.IHelloWorld;

@WebService(endpointInterface = "com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
	public String sayHello(String text) {
		return "Hello : " + text;
	}
}

3)建立spring配置文件,将服务类加入到容器中app

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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.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" />
	
	<!--下面的class属性值必定要跟你项目中服务实现类的包路径彻底一致-->
	<bean id="hello" class="com.service.impl.HelloWorldImpl"/>
	<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>

在web.xml中添加webservice.xml配置文件测试

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/WEB-INF/webservice.xml		
	</param-value>
</context-param>

4)在web.xml中加入cxf servleturl

<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>/webservice/*</url-pattern>
</servlet-mapping>

至此,webservice服务器就建立好了,

在浏览器中访问:http://localhost:8080/test/webservice/HelloWorld?wsdl,(test是项目名称)。若是出现了相似与

<wsdl:definitions xmlns:ns1="http://service.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.service.com/">
。。。。

就配置成功了。

接下来贴几个运行时的错误解决方法

1:webservice.xml中提示cxf.xml,cxf-servlet.xml not found,我在上面写的路径是classpath*:META-INF/cxf/cxf.xml,这里的classpath后面还跟了一个“*”符号,没加符号表示只在类路径下查找cxf.xml文件,加了号表示不只在类路径下查找xml文件,还在jar包中查找xml文件。因此当咱们在项目类路径下没有假如cxf.xml等配置文件时,就必定要在classpath后加*,这样spring容器才会去所加入的jar包中找。

2:假如cxf servlet时,映射路径不要写成/*,不然会出现访问不了项目主页的状况,能够写成/webservice/*或者别的项目中没有使用过的路径来做为cxf servlet的请求路径。

二:建立webservice客户端

客户端能够和服务器放在同一个项目中用来测试,也能够新建一个java项目来进行测试。

新建一个Java项目测试时,要假如对应的jar包,跟服务器同样,使用spring还要假如spring jar包。

我在这里新建一个项目,依旧使用spring来测试

1)首先要建立一个和服务器端同样的服务接口,(若是客户端和服务器端在同一个项目中则能够省略这步)

package com.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(@WebParam(name = "arg0") String text);
}

2)建立spring-client.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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schema/jaxws.xsd">

	<bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.service.IHelloWorld" />
		<property name="address" value="http://localhost:8080/test/HelloWorld" />
	</bean>
</beans>

3)测试类

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.IHelloWorld;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
		IHelloWorld client = (IHelloWorld) ctx.getBean("client");
		String result = client.sayHello("你好!");
		System.out.println(result);
	}
}

运行成功后显示 Hello:你好!

相关文章
相关标签/搜索