首页 → 名言 → 正能量文章
java中怎么傳一個對象參數 java傳輸數據的方式
日期:2023-02-24 07:45:33    编辑:网络投稿    来源:网络资源
如何傳輸Java對象  JavaEE 是 J2EE的一個新的名稱,之所以改名,目的還是讓大家清楚J2EE只是Java企業應用。下面yjbys小編為大家準備了關于如何傳輸Java對象的文章,歡迎閱讀。
为你推荐:
  • 和閨蜜告別的說說
  • 如何傳輸Java對象

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

    如何傳輸Java對象

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

      package com.googlecode.garbagecan.cxfstudy.jaxws;

      import java.util.Date;

      public class Customer {

      private String id;

      private String name;

      private Date birthday;

      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 Date getBirthday() {

      return birthday;

      }

      public void setBirthday(Date birthday) {

      this.birthday = birthday;

      }

      @Override

      public String toString() {

      return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);

      }

      }

      2. 創建Web Service接口類

      package com.googlecode.garbagecan.cxfstudy.jaxws;

      import javax.jws.WebMethod;

      import javax.jws.WebParam;

      import javax.jws.WebResult;

      import javax.jws.WebService;

      @WebService

      public interface CustomerService {

      @WebMethod

      @WebResult Customer findCustomer(@WebParam String id);

      }

      3. 創建Web Service接口的實現類

      package com.googlecode.garbagecan.cxfstudy.jaxws;

      import java.util.Calendar;

      public class CustomerServiceImpl implements CustomerService {

      public Customer findCustomer(String id) {

      Customer customer = new Customer();

      customer.setId("customer_" + id);

      customer.setName("customer_name");

      customer.setBirthday(Calendar.getInstance().getTime());

      return customer;

      }

      }

      4. 下面是Server端的代碼

      package com.googlecode.garbagecan.cxfstudy.jaxws;

      import javax.xml.ws.Endpoint;

      import org.apache.cxf.interceptor.LoggingInInterceptor;

      import org.apache.cxf.interceptor.LoggingOutInterceptor;

      import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

      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();

      factoryBean.getInInterceptors().add(new LoggingInInterceptor());

      factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

      factoryBean.setServiceClass(CustomerServiceImpl.class);

      factoryBean.setAddress(address);

      factoryBean.create();

      }

      }

      5. 下面是Client端的代碼

      package com.googlecode.garbagecan.cxfstudy.jaxws;

      import java.net.SocketTimeoutException;

      import javax.xml.ws.WebServiceException;

      import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

      public class MyClient {

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

      JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

      factoryBean.setAddress("http://localhost:9000/ws/jaxws/customerService");

      factoryBean.setServiceClass(CustomerService.class);

      Object obj = factoryBean.create();

      CustomerService customerService = (CustomerService) obj;

      try {

      Customer customer = customerService.findCustomer("123");

      System.out.println("Customer: " + customer);

      } catch(Exception e) {

      if (e instanceof WebServiceException

      && e.getCause() instanceof SocketTimeoutException) {

      System.err.println("This is timeout exception.");

      } else {

      e.printStackTrace();

      }

      }

      }

      }

      6.測試

      首先運行MyServer類,然后運行MyClient類來驗證Web Service。

    【如何傳輸Java對象】相關文章:

    1.java調用linux命令傳輸遠程大文件

    2.Java面向對象設計的經驗原則

    3.關于java面向對象編程

    4.JAVA認證開源技術:關于Java的對象equals方法

    5.Java如何調用WebService

    6.如何讓JAVA代碼更高效

    7.Java接口如何定義方法

    8.關于Java Socket網絡傳輸的序列化機制

    这里有更多你想看的
  • 朋友圈一個人的說說
    • 本类最新
    • 精品图文
    • 时尚
    • 新闻
    • 生活
    • 视觉
    • 微爱
      栏目ID=88的表不存在(操作类型=0)

    头条推荐

    热门推荐

    特别推荐

    返回顶部