我是靠谱客的博主 整齐哑铃,这篇文章主要介绍java代码调用使用cxf搭建的webService服务传递对象,现在分享给大家,希望可以做个参考。

前边成功创建好一个cxf的webServcie服务,并带了一个无参数的方法。现在进一步尝试了使用带参数的方法,分别测了用String为参数和用自定义的对象为参数。


其中,使用String为参数时和不带参数的写法没有什么区别,调用时也就是很普通的调用。但是调用自定义对象为参数的方法时,实际数据并没有传递成功,于是在网上搜索一番后找到了解决办法,成功实现这一功能。


具体实现步骤大致如下:

1、在服务项目中添加一个自定义的类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package models; public class UserModel { public UserModel() { super(); } public UserModel(String name, int age) { super(); this.name = name; this.age = age; } /** * 姓名 */ private String name; /** * 年龄 */ private int age; @Override public String toString() { return "UserModel [name=" + name + ", age=" + age + "]"; } }

2、服务中添加一个方法:
复制代码
1
public UserModel addUser(@WebParam(name = "user") UserModel user);

3、该方法的实现:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** * 自定义对象为参数 * * @author:tuzongxun * @Title: addUser * @Description: TODO * @param @param user * @param @return * @date Jun 12, 2016 9:28:38 AM * @throws */ @Override public UserModel addUser(UserModel user) { System.out.println(user); return user; }

4、在模拟客户端的项目中也同时添加包名路径相同的自定义UserModel类(因为之前在xfire上,因包名路径不同的问题被坑过,因此这里直接就这样写了,便没有去测是不是也必须相同),在相关的service中添加和服务中一样的方法(代码略过);

5、在模拟客户端的main方法中调用,和之前的写法一样,只是改个调用的方法:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cxfTest1; import models.UserModel; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class CxfTest { public static void main(String[] args) { JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); // factoryBean.getInInterceptors().add(new LoggingInInterceptor()); // factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.setServiceClass(CxfTestService.class); factoryBean .setAddress("http://localhost:8082/cxfTest/services/CxfTestService"); CxfTestService impl = (CxfTestService) factoryBean.create(); // System.out.println(impl.getUserName()); // System.out.println(impl.addUserName("hello")); System.out.println(impl.addUser(new UserModel("uName", 23))); } }

6、结果打印出来的如图,数据传递实际是失败的:



7、之后在网上查找资料后找到解决办法,在自定义类上加上一些注解后,成功传递和返回自定义对象的数据,修改后的自定义对象如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package models; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "UserModel") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = { "name", "age" }) public class UserModel { public UserModel() { super(); } public UserModel(String name, int age) { super(); this.name = name; this.age = age; } /** * 姓名 */ private String name; /** * 年龄 */ private int age; @Override public String toString() { return "UserModel [name=" + name + ", age=" + age + "]"; } }

8、再次在main中调用后,控制台打印如图:


9、上边的注解相关的解释如下:

@XmlRootElement-指定XML根元素名称(可选) 

@XmlAccessorType-控制属性或方法序列化 

四种方案: 

FIELD-对每个非静态,非瞬变属性JAXB工具自动绑定成XML,除非注明XmlTransient 

NONE-不做任何处理 

PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient 

PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注 

明XmlTransient 

@XmlType-映射一个类或一个枚举类型成一个XML Schema类型 


10、参考:http://hbiao68.iteye.com/blog/2044252


最后

以上就是整齐哑铃最近收集整理的关于java代码调用使用cxf搭建的webService服务传递对象的全部内容,更多相关java代码调用使用cxf搭建内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部