Json view support for JMS
Support of @JsonView on @JmsListener annotated method that uses the jackson converter. Also update MappingJackson2MessageConverter to offer a public API to set the JSON view to use to serialize a payload. Issue: SPR-13237
This commit is contained in:
@@ -27,6 +27,7 @@ import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
import javax.jms.Topic;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -236,7 +237,21 @@ public class MessagingMessageListenerAdapterTests {
|
||||
verify(reply).setObjectProperty("foo", "bar");
|
||||
}
|
||||
|
||||
private TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException {
|
||||
@Test
|
||||
public void replyJacksonMessageAndJsonView() throws JMSException {
|
||||
TextMessage reply = testReplyWithJackson("replyJacksonMessageAndJsonView",
|
||||
"{\"name\":\"Response\"}");
|
||||
verify(reply).setObjectProperty("foo", "bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replyJacksonPojoAndJsonView() throws JMSException {
|
||||
TextMessage reply = testReplyWithJackson("replyJacksonPojoAndJsonView",
|
||||
"{\"name\":\"Response\"}");
|
||||
verify(reply, never()).setObjectProperty("foo", "bar");
|
||||
}
|
||||
|
||||
public TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException {
|
||||
Queue replyDestination = mock(Queue.class);
|
||||
|
||||
Session session = mock(Session.class);
|
||||
@@ -327,6 +342,17 @@ public class MessagingMessageListenerAdapterTests {
|
||||
.setHeader("foo", "bar").build();
|
||||
}
|
||||
|
||||
@JsonView(Summary.class)
|
||||
public Message<SampleResponse> replyJacksonMessageAndJsonView(Message<String> input) {
|
||||
return MessageBuilder.withPayload(createSampleResponse(input.getPayload()))
|
||||
.setHeader("foo", "bar").build();
|
||||
}
|
||||
|
||||
@JsonView(Summary.class)
|
||||
public SampleResponse replyJacksonPojoAndJsonView(Message<String> input) {
|
||||
return createSampleResponse(input.getPayload());
|
||||
}
|
||||
|
||||
private SampleResponse createSampleResponse(String name) {
|
||||
return new SampleResponse(name, "lengthy description");
|
||||
}
|
||||
@@ -340,15 +366,22 @@ public class MessagingMessageListenerAdapterTests {
|
||||
}
|
||||
}
|
||||
|
||||
interface Summary {};
|
||||
interface Full extends Summary {};
|
||||
|
||||
private static class SampleResponse {
|
||||
|
||||
private int counter = 42;
|
||||
|
||||
@JsonView(Summary.class)
|
||||
private String name;
|
||||
|
||||
@JsonView(Full.class)
|
||||
private String description;
|
||||
|
||||
SampleResponse() {
|
||||
}
|
||||
|
||||
public SampleResponse(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,28 +17,39 @@
|
||||
package org.springframework.jms.support.converter;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.jms.BytesMessage;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class MappingJackson2MessageConverterTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private MappingJackson2MessageConverter converter;
|
||||
|
||||
private Session sessionMock;
|
||||
@@ -167,6 +178,91 @@ public class MappingJackson2MessageConverterTests {
|
||||
assertEquals("Invalid result", result, unmarshalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithReturnType() throws JMSException, NoSuchMethodException {
|
||||
Method method = this.getClass().getDeclaredMethod("summary");
|
||||
MethodParameter returnType = new MethodParameter(method, -1);
|
||||
testToTextMessageWithReturnType(returnType);
|
||||
verify(sessionMock).createTextMessage("{\"name\":\"test\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithNullReturnType() throws JMSException, NoSuchMethodException {
|
||||
testToTextMessageWithReturnType(null);
|
||||
verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithReturnTypeAndNoJsonView() throws JMSException, NoSuchMethodException {
|
||||
Method method = this.getClass().getDeclaredMethod("none");
|
||||
MethodParameter returnType = new MethodParameter(method, -1);
|
||||
|
||||
testToTextMessageWithReturnType(returnType);
|
||||
verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithReturnTypeAndMultipleJsonViews() throws JMSException, NoSuchMethodException {
|
||||
Method method = this.getClass().getDeclaredMethod("invalid");
|
||||
MethodParameter returnType = new MethodParameter(method, -1);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
testToTextMessageWithReturnType(returnType);
|
||||
}
|
||||
|
||||
private void testToTextMessageWithReturnType(MethodParameter returnType) throws JMSException, NoSuchMethodException {
|
||||
converter.setTargetType(MessageType.TEXT);
|
||||
TextMessage textMessageMock = mock(TextMessage.class);
|
||||
|
||||
MyAnotherBean bean = new MyAnotherBean("test", "lengthy description");
|
||||
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
|
||||
converter.toMessage(bean, sessionMock, returnType);
|
||||
verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithJsonViewClass() throws JMSException {
|
||||
converter.setTargetType(MessageType.TEXT);
|
||||
TextMessage textMessageMock = mock(TextMessage.class);
|
||||
|
||||
MyAnotherBean bean = new MyAnotherBean("test", "lengthy description");
|
||||
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
|
||||
|
||||
|
||||
converter.toMessage(bean, sessionMock, Summary.class);
|
||||
verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName());
|
||||
verify(sessionMock).createTextMessage("{\"name\":\"test\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toTextMessageWithAnotherJsonViewClass() throws JMSException {
|
||||
converter.setTargetType(MessageType.TEXT);
|
||||
TextMessage textMessageMock = mock(TextMessage.class);
|
||||
|
||||
MyAnotherBean bean = new MyAnotherBean("test", "lengthy description");
|
||||
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
|
||||
|
||||
|
||||
converter.toMessage(bean, sessionMock, Full.class);
|
||||
verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName());
|
||||
verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}");
|
||||
}
|
||||
|
||||
|
||||
@JsonView(Summary.class)
|
||||
public MyAnotherBean summary() {
|
||||
return new MyAnotherBean();
|
||||
}
|
||||
|
||||
public MyAnotherBean none() {
|
||||
return new MyAnotherBean();
|
||||
}
|
||||
|
||||
@JsonView({Summary.class, Full.class})
|
||||
public MyAnotherBean invalid() {
|
||||
return new MyAnotherBean();
|
||||
}
|
||||
|
||||
public static class MyBean {
|
||||
|
||||
public MyBean() {
|
||||
@@ -210,4 +306,40 @@ public class MappingJackson2MessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
private interface Summary {};
|
||||
private interface Full extends Summary {};
|
||||
|
||||
private static class MyAnotherBean {
|
||||
|
||||
@JsonView(Summary.class)
|
||||
private String name;
|
||||
|
||||
@JsonView(Full.class)
|
||||
private String description;
|
||||
|
||||
private MyAnotherBean() {
|
||||
}
|
||||
|
||||
public MyAnotherBean(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user