當前位置:文思屋>社會工作>IT認證>

如何傳輸Java物件

文思屋 人氣:1.98W

JavaEE 是 J2EE的一個新的名稱,之所以改名,目的還是讓大家清楚J2EE只是Java企業應用。下面本站小編為大家準備了關於如何傳輸Java物件的文章,歡迎閱讀。

如何傳輸Java物件

1. 首先是一個普通的pojo物件,用來表示一個實體類

package s;

import ;

public class Customer {

private String id;

private String name;

private Date birthday;

public String getId() {

return id;

}

public void setId(String id) {

= id;

}

public String getName() {

return name;

}

public void setName(String name) {

= name;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

hday = birthday;

}

@Override

public String toString() {

return ectionToString(this);

}

}

2. 建立Web Service介面類

package s;

import ethod;

import aram;

import esult;

import ervice;

@WebService

public interface CustomerService {

@WebMethod

@WebResult Customer findCustomer(@WebParam String id);

}

3. 建立Web Service介面的.實現類

package s;

import ndar;

public class CustomerServiceImpl implements CustomerService {

public Customer findCustomer(String id) {

Customer customer = new Customer();

d("customer_" + id);

ame("customer_name");

irthday(nstance()ime());

return customer;

}

}

4. 下面是Server端的程式碼

package s;

import oint;

import ingInInterceptor;

import ingOutInterceptor;

import sServerFactoryBean;

public class MyServer {

private static final String address = "http://localhost:9000/ws/jaxws/customerService";

public static void main(String[] args) throws Exception {

// http://localhost:9000/ws/jaxws/customerService?wsdl

JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();

nInterceptors()(new LoggingInInterceptor());

utInterceptors()(new LoggingOutInterceptor());

erviceClass(s);

ddress(address);

te();

}

}

5. 下面是Client端的程式碼

package s;

import etTimeoutException;

import erviceException;

import sProxyFactoryBean;

public class MyClient {

public static void main(String[] args) throws Exception {

JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

ddress("http://localhost:9000/ws/jaxws/customerService");

erviceClass(s);

Object obj = te();

CustomerService customerService = (CustomerService) obj;

try {

Customer customer = Customer("123");

tln("Customer: " + customer);

} catch(Exception e) {

if (e instanceof WebServiceException

&& ause() instanceof SocketTimeoutException) {

tln("This is timeout exception.");

} else {

tStackTrace();

}

}

}

}

6.測試

首先執行MyServer類,然後執行MyClient類來驗證Web Service。

TAGS:Java 傳輸