com.foreveross.springboot.dubbo.utils.Payload Maven / Gradle / Ivy
package com.foreveross.springboot.dubbo.utils;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.UUID;
public class Payload implements Serializable {
private static final long serialVersionUID = -1549643581827130116L;
@JsonProperty("payload")
private Object payload;
@JsonProperty("request_id")
@Value("${dubbox.service.request_id}")
private String request_id = UUID.randomUUID().toString();
private JsonFilterConfig mapper;
private ObjectMapper objectMapper = new ObjectMapper();
public Payload() {
}
public Payload(Object payload) {
this.payload = payload;
}
public Object getPayload() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
return this.mapper.filteredWriter(this.payload);
}
public void setPayload(Object payload) {
this.payload = payload;
}
public String toString() {
try {
String json = (this.payload != null ? objectMapper.writeValueAsString(this) : "{payload=" + null + ",request_id=" + request_id + "}");
return this.payload != null ? objectMapper.readValue(json, Map.class).toString() : json;
} catch (IOException e) {
e.printStackTrace();
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
}
}