java对象作为参数传递,java传输对象_如何传输Java对象

 2023-09-22 阅读 20 评论 0

摘要:如何传输Java对象JavaEE 是 J2EE的一个新的名称,之所以改名,目的还是让大家清楚J2EE只是Java企业应用。下面yjbys小编为大家准备了关于如何传输Java对象的文章,欢迎阅读。java对象作为参数传递。1. 首先是一个普通的.pojo对象,用来表示一个实体类

如何传输Java对象

JavaEE 是 J2EE的一个新的名称,之所以改名,目的还是让大家清楚J2EE只是Java企业应用。下面yjbys小编为大家准备了关于如何传输Java对象的文章,欢迎阅读。

7e1089dfd6c115948c97408f619905ff.png

java对象作为参数传递。1. 首先是一个普通的.pojo对象,用来表示一个实体类

package com.googlecode.garbagecan.cxfstudy.jaxws;

import java.util.Date;

java对象的定义。public class Customer {

private String id;

private String name;

java对象是值传递还是引用传递?private Date birthday;

public String getId() {

return id;

java中类和对象的关系。}

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对象】相关文章:

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/86412.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息