maven+spring+cxf编写web service

参见文章
1.创建项目

mvn archetype:generate -DarchetypeCatalog=Internal
选择19,创建web项目

2.生成eclipse项目,参见文章
3.修改web.xml




	Archetype Created Web Application
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		contextConfigLocation
		WEB-INF/applicationContext.xml
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
		CXFService
		org.apache.cxf.transport.servlet.CXFServlet
	

	
		CXFService
		/ws/*
	

4.创建webservice接口
package com.sysware.demo.app.service.inf;

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

import com.sysware.demo.app.model.RetInfo;

@WebService
public interface IGetInfoService
{
	@WebMethod(operationName = "add")
	@WebResult(name = "result")
	public int add(@WebParam(name = "num1") int num1,
			@WebParam(name = "num2") int num2);
	
	@WebMethod(operationName = "getRetInfo")
    @WebResult(name = "result")
    public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);
}
5.创建webservice实现类
package com.sysware.demo.app.service.impl;

import javax.jws.WebService;

import com.sysware.demo.app.model.RetInfo;
import com.sysware.demo.app.service.inf.IGetInfoService;

@WebService(endpointInterface = "com.sysware.demo.app.service.inf.IGetInfoService")
public class GetInfoServiceImpl implements IGetInfoService
{

	@Override
	public int add(int num1, int num2)
	{
		return num1 + num2;
	}

	@Override
	public RetInfo getRetInfo(String name, int age)
	{
        RetInfo retInfo = new RetInfo();
        retInfo.setAge(age);
        retInfo.setName(name);
        return retInfo;
	}

}
6.返回对象
package com.sysware.demo.app.model;

public class RetInfo
{
    private String name;
    private int age;
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
}
7.创建bean文件applicationContext.xml在WEB-INF目录下


	
	
		
	
	
8.修改pom文件
	4.0.0
	com.sysware.demo
	mvncxfdemo
	war
	1.0-SNAPSHOT
	mvncxfdemo Maven Webapp
	http://maven.apache.org	
        
		
			junit
			junit
			4.10
			jar
			test
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			2.6.1
		
		
			org.springframework
			spring-context
			3.1.2.RELEASE
		
		
			org.springframework
			spring-web
			3.1.2.RELEASE
		
		
			org.apache.cxf
			cxf-rt-transports-common
			2.5.4
			jar
			compile
		
		
			org.apache.cxf
			cxf-rt-core
			2.6.1
			jar
			compile
		
		
			org.apache.cxf
			cxf-rt-transports-http-jetty
			2.6.1
			jar
			compile
		
	
	
		mvncxfdemo
		
			
				org.mortbay.jetty
				jetty-maven-plugin
				8.1.5.v20120716
				
					9966
					foo
				
			
		
	
	
		UTF-8
	
9.运行
mvn clean jetty:run-war

10.或者实现接口修改为

package com.sysware.demo.app.service.impl;

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

import com.sysware.demo.app.model.RetInfo;

@WebService
public class GetInfoServiceImpl
{

	@WebMethod(operationName = "add")
	@WebResult(name = "result")
	public int add(@WebParam(name = "num1") int num1,
			@WebParam(name = "num2") int num2)
	{
		return num1 + num2;
	}

	@WebMethod(operationName = "getRetInfo")
    @WebResult(name = "result")
    public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age)
	{
		RetInfo retInfo = new RetInfo();
		retInfo.setAge(age);
		retInfo.setName(name);
		return retInfo;
	}

}

由于客户端用gsoap,如果两个service在同一个目录下,将导致错误,因为两个targetNamespace相同


为了保证两个service不在一个target里面,建议每个package下只有一个webservice

你可能感兴趣的:(安装配置,Java)