我有两个简单的测试应用程序:服务器和客户端。服务器使用org.springframework.jms.remoting.JmsInvokerServiceExporter
公开用户服务:
public interface UserService {
void registerUser(User user);
}
@Component("userService")
public class UserServiceImpl implements UserService {
@Override
public void registerUser(User user) {
// Do something
}
}
public class User implements Serializable {
private String firstName;
private String lastName;
private String email;
private String password;
// Getters and setters
}
@Configuration
public class JmsConfig {
@Bean
public ConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory();
}
@Bean
public Queue userQueue() {
return new ActiveMQQueue("user.queue");
}
@Bean
public MessageConverter jmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTypeIdPropertyName("JmsMessageType");
return converter;
}
@Bean
public JmsInvokerServiceExporter userServiceExporter(MessageConverter messageConverter, UserService userService) {
JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter();
exporter.setMessageConverter(messageConverter);
exporter.setService(userService);
exporter.setServiceInterface(UserService.class);
return exporter;
}
@Bean
public AbstractMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory, Destination destination, MessageConverter messageConverter, SessionAwareMessageListener messageListener) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setDestination(destination);
container.setMessageConverter(messageConverter);
container.setMessageListener(messageListener);
return container;
}
}
客户使用org.springframework.jms.remoting.JmsInvokerProxyFactoryBean
消费用户服务:
@Bean
public JmsInvokerProxyFactoryBean userService(ConnectionFactory connectionFactory, Queue queue, MessageConverter messageConverter) {
JmsInvokerProxyFactoryBean jmsInvoker = new JmsInvokerProxyFactoryBean();
jmsInvoker.setConnectionFactory(connectionFactory);
jmsInvoker.setQueue(queue);
jmsInvoker.setMessageConverter(messageConverter);
jmsInvoker.setServiceInterface(UserService.class);
return jmsInvoker;
}
在服务器端,我遇到以下异常:
java.lang.Throwable: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.remoting.support.RemoteInvocation.invoke
经过一番探索后,我发现User对象没有从JSON字符串中正确反序列化。 Spring尝试使用registerUser
作为参数调用Map
方法,该参数包含客户端传递的User对象的值。
在Spring JMS RPC的情况下,是否可以对自定义对象进行自动JSON序列化/反序列化?