CXF之客户端调用

一、返回类型为Map问题

cxf的restful实现已经实现返回类型为Map,不需要做任何的转换。


二、参数为Map问题

因为cxf不直接支持参数为Map情况,所以需要我们定义一个类型转换适配器

package com.winssage.base.module.frameworkimpl.security.util;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * 
 * 类型转换适配器类
 * 
 * @author limanman
 * 
 */
public class MapAdapter extends XmlAdapter<MapConvertor, Map<String, Object>> {

	@Override
	public MapConvertor marshal(Map<String, Object> map) throws Exception {
		MapConvertor convertor = new MapConvertor();
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			MapConvertor.MapEntry e = new MapConvertor.MapEntry(entry);
			convertor.addEntry(e);
		}
		return convertor;
	}

	@Override
	public Map<String, Object> unmarshal(MapConvertor map) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		for (MapConvertor.MapEntry e : map.getEntries()) {
			result.put(e.getKey(), e.getValue());
		}
		return result;
	}

}



package com.winssage.base.module.frameworkimpl.security.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * Map格式转换类
 * 
 * @author limanman
 * 
 */
@XmlRootElement 
@XmlType(name = "MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapConvertor {

	private List<MapEntry> entries = new ArrayList<MapEntry>();

	public void addEntry(MapEntry entry) {
		entries.add(entry);
	}

	public static class MapEntry {
		public MapEntry() {
			super();
		}

		public MapEntry(Map.Entry<String, Object> entry) {
			super();
			this.key = entry.getKey();
			this.value = entry.getValue();
		}

		public MapEntry(String key, Object value) {
			super();
			this.key = key;
			this.value = value;
		}

		private String key;
		private Object value;

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}
	}

	public List<MapEntry> getEntries() {
		return entries;
	}
}



三、例子

package com.winssage.winssagebpm.service;

import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.winssage.base.module.frameworkimpl.security.util.MapAdapter;

@Path(value = "/jbpm")
public interface BpmService {
	/**
	 * 获取任务的变量定义
	 * 
	 * @param taskId 任务ID
	 * 
	 * @return
	 */
	@POST
	@Path("/getVariablesByTaskId")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Map<String, Object> getVariablesByTaskId(String taskId);

	@POST
	@Path("/map")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@XmlJavaTypeAdapter(MapAdapter.class)
	public Map<String, Object> testMap(
			@XmlJavaTypeAdapter(MapAdapter.class) Map<String, Object> map);
}



package com.winssage.winssagebpm.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Named;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.winssage.winssagebpm.service.BpmService;

@Named("bpmService")
@Transactional
public class BpmServiceImpl implements BpmService {
	private Logger log = Logger.getLogger(this.getClass());

	@Override
	public Map<String, Object> getVariablesByTaskId(String taskId) {
		log.info("taskId: " + taskId);

		// Task task = this.taskService.getTask(taskId);
		// TaskImpl taskImpl = (TaskImpl) task;
		// Map<String, Object> variables = taskImpl.getVariables();
		// return variables;
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("name", "fengshu");
		return variables;
	}

	public Map<String, Object> testMap(Map<String, Object> map) {
		map.put("password", 123);
		return map;
	}
}



转载于:https://my.oschina.net/fengshuzi/blog/307261

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值