經過restTemplate發送請求,特定類型參數沒法被解析的問題

客戶端經過RestTemplate發送請求,requestBody是一個複雜數據類型,經過嵌套多層MultiValueMap的方式組合服務端須要的數據類型。html

HttpEntity requestBody = generateAddAllRequestBody(strategyJobs);
ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(jobAddAllUrl, requestBody, String.class);
private HttpEntity generateAddAllRequestBody(List<StrategyJob> strategyJobs) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, MultiValueMap> outerMap = new LinkedMultiValueMap<>();
        for(StrategyJob strategyJob : strategyJobs) {
            MultiValueMap map = new LinkedMultiValueMap();
            map.add("jobGroup",strategyJob.getGroup());
            map.add("jobDesc", strategyJob.getJobDesc());
            map.add("executorRouteStrategy", "FIRST");
            map.add("cronGen_display", strategyJob.getCron());
            map.add("jobCron", strategyJob.getCron());
            map.add("glueType", "BEAN");
            map.add("executorHandler", strategyJob.getExecutorHandler());
            map.add("executorBlockStrategy", "SERIAL_EXECUTION");
            map.add("childJobId", "");
            map.add("executorTimeout", 0);
            map.add("executorFailRetryCount", 0);
            map.add("author", strategyJob.getCreator());
            map.add("alarmEmail", "");
            map.add("executorParam", strategyJob.getStrategyId());
            map.add("glueRemark", "GLUE代碼初始化");
            map.add("glueSource", "");
            map.add("triggerStatus", 1);

            if (strategyJob.getId() != null) {
                map.add("id", strategyJob.getId());
            }
            outerMap.add(strategyJob.getKey(), map);
        }
        HttpEntity requestBody = new HttpEntity(outerMap, headers);
        return requestBody;
    }

服務端接收請求的時候,只能解析第一層的key-value值,value中的複雜數據類型沒法解析,發生異常,此時須要在服務端增長一個Converter,將默認的String類型轉成對應的複雜類型:java

package com.xxl.job.admin.core.converter;

import com.alibaba.fastjson.JSON;
import com.xxl.job.admin.core.model.XxlJobInfo;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class String2XxlJobInfo implements Converter<String, XxlJobInfo> {

    @Override
    public XxlJobInfo convert(String s) {
        String ss = s.replaceAll("[\\[\\]]", "");
        XxlJobInfo xxlJobInfo = JSON.parseObject(ss, XxlJobInfo.class);
        return xxlJobInfo;
    }
}

服務端的controller方法爲:spring

@RequestMapping("/addStrategyJobs")
	@ResponseBody
	@PermissionLimit(limit=false)
	public ReturnT<List<String>> addStrategyJobs(StrategyJobInfo strategyJobInfo) {
		return xxlJobService.addStrategyJobs(strategyJobInfo);
	}

其中StrategyJobInfo的代碼如:json

package com.xxl.job.admin.core.model;

public class StrategyJobInfo {

    XxlJobInfo preApplyJob;
    XxlJobInfo applyJob;
    XxlJobInfo preRestoreJob;
    XxlJobInfo restoreJob;

    public XxlJobInfo getPreApplyJob() {
        return preApplyJob;
    }

    public void setPreApplyJob(XxlJobInfo preApplyJob) {
        this.preApplyJob = preApplyJob;
    }

    public XxlJobInfo getApplyJob() {
        return applyJob;
    }

    public void setApplyJob(XxlJobInfo applyJob) {
        this.applyJob = applyJob;
    }

    public XxlJobInfo getPreRestoreJob() {
        return preRestoreJob;
    }

    public void setPreRestoreJob(XxlJobInfo preRestoreJob) {
        this.preRestoreJob = preRestoreJob;
    }

    public XxlJobInfo getRestoreJob() {
        return restoreJob;
    }

    public void setRestoreJob(XxlJobInfo restoreJob) {
        this.restoreJob = restoreJob;
    }
}

XxlJobInfo的代碼如:app

package com.xxl.job.admin.core.model;

import java.util.Date;

/**
 * xxl-job info
 *
 * @author xuxueli  2016-1-12 18:25:49
 */
public class XxlJobInfo {
	
	private int id;				// 主鍵ID
	
	private int jobGroup;		// 執行器主鍵ID
	private String jobCron;		// 任務執行CRON表達式
	private String jobDesc;
	
	private Date addTime;
	private Date updateTime;
	
	private String author;		// 負責人
	private String alarmEmail;	// 報警郵件

	private String executorRouteStrategy;	// 執行器路由策略
	private String executorHandler;		    // 執行器,任務Handler名稱
	private String executorParam;		    // 執行器,任務參數
	private String executorBlockStrategy;	// 阻塞處理策略
	private int executorTimeout;     		// 任務執行超時時間,單位秒
	private int executorFailRetryCount;		// 失敗重試次數
	
	private String glueType;		// GLUE類型	#com.xxl.job.core.glue.GlueTypeEnum
	private String glueSource;		// GLUE源代碼
	private String glueRemark;		// GLUE備註
	private Date glueUpdatetime;	// GLUE更新時間

	private String childJobId;		// 子任務ID,多個逗號分隔

	private int triggerStatus;		// 調度狀態:0-中止,1-運行
	private long triggerLastTime;	// 上次調度時間
	private long triggerNextTime;	// 下次調度時間


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(int jobGroup) {
		this.jobGroup = jobGroup;
	}

	public String getJobCron() {
		return jobCron;
	}

	public void setJobCron(String jobCron) {
		this.jobCron = jobCron;
	}

	public String getJobDesc() {
		return jobDesc;
	}

	public void setJobDesc(String jobDesc) {
		this.jobDesc = jobDesc;
	}

	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAlarmEmail() {
		return alarmEmail;
	}

	public void setAlarmEmail(String alarmEmail) {
		this.alarmEmail = alarmEmail;
	}

	public String getExecutorRouteStrategy() {
		return executorRouteStrategy;
	}

	public void setExecutorRouteStrategy(String executorRouteStrategy) {
		this.executorRouteStrategy = executorRouteStrategy;
	}

	public String getExecutorHandler() {
		return executorHandler;
	}

	public void setExecutorHandler(String executorHandler) {
		this.executorHandler = executorHandler;
	}

	public String getExecutorParam() {
		return executorParam;
	}

	public void setExecutorParam(String executorParam) {
		this.executorParam = executorParam;
	}

	public String getExecutorBlockStrategy() {
		return executorBlockStrategy;
	}

	public void setExecutorBlockStrategy(String executorBlockStrategy) {
		this.executorBlockStrategy = executorBlockStrategy;
	}

	public int getExecutorTimeout() {
		return executorTimeout;
	}

	public void setExecutorTimeout(int executorTimeout) {
		this.executorTimeout = executorTimeout;
	}

	public int getExecutorFailRetryCount() {
		return executorFailRetryCount;
	}

	public void setExecutorFailRetryCount(int executorFailRetryCount) {
		this.executorFailRetryCount = executorFailRetryCount;
	}

	public String getGlueType() {
		return glueType;
	}

	public void setGlueType(String glueType) {
		this.glueType = glueType;
	}

	public String getGlueSource() {
		return glueSource;
	}

	public void setGlueSource(String glueSource) {
		this.glueSource = glueSource;
	}

	public String getGlueRemark() {
		return glueRemark;
	}

	public void setGlueRemark(String glueRemark) {
		this.glueRemark = glueRemark;
	}

	public Date getGlueUpdatetime() {
		return glueUpdatetime;
	}

	public void setGlueUpdatetime(Date glueUpdatetime) {
		this.glueUpdatetime = glueUpdatetime;
	}

	public String getChildJobId() {
		return childJobId;
	}

	public void setChildJobId(String childJobId) {
		this.childJobId = childJobId;
	}

	public int getTriggerStatus() {
		return triggerStatus;
	}

	public void setTriggerStatus(int triggerStatus) {
		this.triggerStatus = triggerStatus;
	}

	public long getTriggerLastTime() {
		return triggerLastTime;
	}

	public void setTriggerLastTime(long triggerLastTime) {
		this.triggerLastTime = triggerLastTime;
	}

	public long getTriggerNextTime() {
		return triggerNextTime;
	}

	public void setTriggerNextTime(long triggerNextTime) {
		this.triggerNextTime = triggerNextTime;
	}
}

參考ide

相關文章
相關標籤/搜索