Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Spring-Messaging Serialization in spring-json-view #56

Open
mikejhill opened this issue Jan 30, 2018 · 0 comments
Open

Support Spring-Messaging Serialization in spring-json-view #56

mikejhill opened this issue Jan 30, 2018 · 0 comments

Comments

@mikejhill
Copy link
Contributor

The JsonViewSerializer should be registered as a MessageConverter for Spring-Messaging serialization.

The team I work with implemented similar serialization logic several years back to allow clients to specify requested properties to reduce bandwidth usage. This works similar to the Squiggly Filter library in that it obtains the requested properties from the HttpServletRequest object itself, which is okay when such an object exists but requires some extra tricks if it doesn't, such as when working with WebSockets. The technique used in JsonView comes in handy for this. We can wrap entities in configured JsonView when sending messages and register a converter to serialize them accordingly.

The one caveat to this is that spring-json-view registers an appropriate return handler with for the RequestMappingHandlerAdapter, but doesn't do a similar MessageConverter registration to convert Message entities. We currently do this registration manually through the configureMessageConverters method of our AbstractWebSocketMessageBrokerConfigurer implementation.

The simple gist of our implementation using Spring 4.3.14 is as follows:

@Component
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
	@Override
	public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
		messageConverters.add(new JsonViewMessageConverter());
		return true;
	}
}

public class JsonViewMessageConverter extends MappingJackson2MessageConverter {
	public JsonViewMessageConverter() {
		...
	}

	public JsonViewMessageConverter(ObjectMapper mapper) {
		...
	}

	@Override
	protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
		return false;
	}

	@Override
	protected boolean canConvertTo(Object payload, MessageHeaders headers) {
		return payload instanceof JsonView;
	}
}

// Example usage
@Component
public class WebSocketHandler {
	private SimpMessagingTemplate brokerMessagingTemplate;

	@Autowired
	public WebSocketHandler(SimpMessagingTemplate brokerMessagingTemplate) {
		this.brokerMessagingTemplate = brokerMessagingTemplate;
	}

	public void sendMessage(String channel, Object payload, Class<?> onClass, String... fields) {
		JsonView<Object> payloadView = JsonView.with(payload).onClass(onClass, Match.match()
				.exclude("*")
				.include(fields));
		getBrokerMessagingTemplate().convertAndSend(channel, payloadView);
	}
}

It would be helpful for the library to implement the message converter and to automatically register this converter for the user or to create a small separate library to do so in order to prevent unwanted dependencies on spring-messaging.

Although our implementation is not yet using it, the JsonResult functionality might also be implemented via the converter's canConvertTo method.

Lastly, thank you for creating and sharing this project! This is very helpful for the application that we're working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant