在webservice中传输实体对象

1、在服务端新建一个pojo

package com.njupt.cxf.pojo;

public class Person {

	private String id;
	private String name;
	private String address;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String toString() {
		return  id + " " +name + "  "+address ;
	}
}

2、新建pojo的service几impl

1)PersonService

package com.njupt.cxf.service;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import com.njupt.cxf.pojo.Person;

@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public interface PersonService {

	public Person findById(String id);
}

2)PersionServiceImpl

package com.njupt.cxf.service;


import com.njupt.cxf.pojo.Person;

public class PersonServiceImpl implements PersonService {

	public Person findById(String id) {

		Person p = new Person();
		p.setId("001");
		p.setName("章泽天");
		p.setAddress("江苏南京");
		
		return p;
	}

}

3、cfg-servlet.xml

在cfg-servlet.xml中加上

<jaxws:server id="personService" serviceClass="com.njupt.cxf.service.PersonService" address="/person">
		<jaxws:serviceBean>
			<!-- 服务的实现类 -->
			<bean class="com.njupt.cxf.service.PersonServiceImpl"></bean>
		</jaxws:serviceBean>
		<!-- 加入请求的消息拦截器 -->
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<!-- 加入响应的消息拦截器 -->
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>

4、利用wsdl2java生成客户端的调用代码,并粘贴到客户端的项目中

5、App

package com.njupt.cxf.service;

public class App {

	public static void main(String[] args) {
		PersonServiceService pss = new PersonServiceService();
		PersonService ps =  pss.getPersonServicePort();
	    Person p = ps.findById("1");
	    
	    System.out.println(p);
	}
	
}



你可能感兴趣的:(在webservice中传输实体对象)