INT-1530 a custom ObjectMapper can now be provided for the inbound and outbound mappers

This commit is contained in:
Mark Fisher
2010-11-17 23:09:44 -05:00
parent 2d5525a3b9
commit d4ebf43833
2 changed files with 20 additions and 5 deletions

View File

@@ -43,7 +43,8 @@ import org.springframework.util.Assert;
*/
public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
private static final String MESSAGE_FORMAT_ERROR = "JSON message is invalid. Expected a message in the format of either {\"headers\":{...},\"payload\":{...}} or {\"payload\":{...}.\"headers\":{...}} but was ";
private static final String MESSAGE_FORMAT_ERROR = "JSON message is invalid. Expected a message in the format of either " +
"{\"headers\":{...},\"payload\":{...}} or {\"payload\":{...}.\"headers\":{...}} but was ";
private static final Map<String, Class<?>> DEFAULT_HEADER_TYPES = new HashMap<String, Class<?>>();
@@ -54,24 +55,31 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
}
private final ObjectMapper objectMapper = new ObjectMapper();
private final JavaType payloadType;
private final Map<String, Class<?>> headerTypes = DEFAULT_HEADER_TYPES;
private volatile ObjectMapper objectMapper = new ObjectMapper();
private volatile boolean mapToPayload = false;
public JsonInboundMessageMapper(Class<?> payloadType) {
Assert.notNull(payloadType, "payloadType must not be null");
this.payloadType = TypeFactory.type(payloadType);
}
public JsonInboundMessageMapper(TypeReference<?> typeReference) {
Assert.notNull(typeReference, "typeReference must not be null");
this.payloadType = TypeFactory.type(typeReference);
}
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "objectMapper must not be null");
this.objectMapper = objectMapper;
}
public void setHeaderTypes(Map<String, Class<?>> headerTypes) {
this.headerTypes.putAll(headerTypes);
}
@@ -88,7 +96,7 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
}
catch (JsonMappingException ex) {
throw new IllegalArgumentException("Mapping of JSON message " + jsonMessage +
" directly to payload of type " + payloadType.getRawClass().getName() + " failed.", ex);
" directly to payload of type " + this.payloadType.getRawClass().getName() + " failed.", ex);
}
}
else {

View File

@@ -22,20 +22,27 @@ import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.integration.Message;
import org.springframework.integration.mapping.OutboundMessageMapper;
import org.springframework.util.Assert;
/**
* {@link OutboundMessageMapper} implementation the converts a {@link Message} to a JSON string representation.
*
* @author Jeremy Grelle
* @author Mark Fisher
* @since 2.0
*/
public class JsonOutboundMessageMapper implements OutboundMessageMapper<String> {
private volatile boolean shouldExtractPayload = false;
private final ObjectMapper objectMapper = new ObjectMapper();
private volatile ObjectMapper objectMapper = new ObjectMapper();
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "objectMapper must not be null");
this.objectMapper = objectMapper;
}
public void setShouldExtractPayload(boolean shouldExtractPayload) {
this.shouldExtractPayload = shouldExtractPayload;
}