首页 > 代码库 > CXF之客户端调用

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 = http://www.mamicode.com/entry.getValue();> 



三、例子

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 = http://www.mamicode.com/"/jbpm")> 



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;
	}
}



CXF之客户端调用