From 965fca808a2b98987731c2f9b4049d1f2ec8e0ef Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Jul 2015 01:08:16 +0200 Subject: [PATCH] Polishing --- .../springframework/core/MethodParameter.java | 2 +- .../AbstractAdaptableMessageListener.java | 37 +++++++++---------- .../MessagingMessageListenerAdapter.java | 8 ++-- .../annotation/JsonViewRequestBodyAdvice.java | 17 +++++---- .../JsonViewResponseBodyAdvice.java | 15 ++++---- .../socket/sockjs/client/InfoReceiver.java | 3 +- .../web/socket/sockjs/client/Transport.java | 5 +-- .../socket/sockjs/client/XhrTransport.java | 1 + 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 89b8156a0e..171575693a 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -207,7 +207,7 @@ public class MethodParameter { /** * Return the index of the method/constructor parameter. - * @return the parameter index (never negative) + * @return the parameter index (-1 in case of the return type) */ public int getParameterIndex() { return this.parameterIndex; diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java index 9b53d5647b..7603770c3d 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java @@ -40,8 +40,8 @@ import org.springframework.jms.support.destination.DynamicDestinationResolver; import org.springframework.util.Assert; /** - * An abstract {@link MessageListener} adapter providing the necessary infrastructure - * to extract the payload of a {@link Message} + * An abstract JMS {@link MessageListener} adapter providing the necessary + * infrastructure to extract the payload of a JMS {@link Message}. * * @author Juergen Hoeller * @author Stephane Nicoll @@ -217,7 +217,7 @@ public abstract class AbstractAdaptableMessageListener return message; } catch (JMSException ex) { - throw new MessageConversionException("Could not unmarshal message", ex); + throw new MessageConversionException("Could not convert JMS message", ex); } } @@ -246,10 +246,12 @@ public abstract class AbstractAdaptableMessageListener sendResponse(session, destination, response); } catch (Exception ex) { - throw new ReplyFailureException("Failed to send reply with payload '" + result + "'", ex); + throw new ReplyFailureException("Failed to send reply with payload [" + result + "]", ex); } } + else { + // No JMS Session available if (logger.isWarnEnabled()) { logger.warn("Listener method returned result [" + result + "]: not generating response message for it because of no JMS Session given"); @@ -266,25 +268,21 @@ public abstract class AbstractAdaptableMessageListener * @see #setMessageConverter */ protected Message buildMessage(Session session, Object result) throws JMSException { - Object content = (result instanceof JmsResponse - ? ((JmsResponse) result).getResponse() : result); + Object content = (result instanceof JmsResponse ? ((JmsResponse) result).getResponse() : result); + if (content instanceof org.springframework.messaging.Message) { + return this.messagingMessageConverter.toMessage(content, session); + } MessageConverter converter = getMessageConverter(); if (converter != null) { - if (content instanceof org.springframework.messaging.Message) { - return this.messagingMessageConverter.toMessage(content, session); - } - else { - return converter.toMessage(content, session); - } + return converter.toMessage(content, session); } - else { - if (!(content instanceof Message)) { - throw new MessageConversionException( - "No MessageConverter specified - cannot handle message [" + content + "]"); - } - return (Message) content; + + if (!(content instanceof Message)) { + throw new MessageConversionException( + "No MessageConverter specified - cannot handle message [" + content + "]"); } + return (Message) content; } /** @@ -307,6 +305,7 @@ public abstract class AbstractAdaptableMessageListener private Destination getResponseDestination(Message request, Message response, Session session, Object result) throws JMSException { + if (result instanceof JmsResponse) { JmsResponse jmsResponse = (JmsResponse) result; Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); @@ -418,7 +417,7 @@ public abstract class AbstractAdaptableMessageListener if (converter != null) { return converter.toMessage(payload, session); } - throw new IllegalStateException("No message converter, cannot handle '" + payload + "'"); + throw new IllegalStateException("No message converter - cannot handle [" + payload + "]"); } } diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java index 1c8573a428..0791f4c400 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -77,7 +77,7 @@ public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageLis return (Message) getMessagingMessageConverter().fromMessage(jmsMessage); } catch (JMSException ex) { - throw new MessageConversionException("Could not unmarshal message", ex); + throw new MessageConversionException("Could not convert JMS message", ex); } } @@ -90,8 +90,8 @@ public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageLis return this.handlerMethod.invoke(message, jmsMessage, session); } catch (MessagingException ex) { - throw new ListenerExecutionFailedException(createMessagingErrorMessage("Listener method could not " + - "be invoked with the incoming message"), ex); + throw new ListenerExecutionFailedException( + createMessagingErrorMessage("Listener method could not be invoked with incoming message"), ex); } catch (Exception ex) { throw new ListenerExecutionFailedException("Listener method '" + diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewRequestBodyAdvice.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewRequestBodyAdvice.java index 3708e7a02e..255e969a3c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewRequestBodyAdvice.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewRequestBodyAdvice.java @@ -28,22 +28,23 @@ import org.springframework.http.converter.json.AbstractJackson2HttpMessageConver import org.springframework.http.converter.json.MappingJacksonInputMessage; /** - * A {@code RequestBodyAdvice} implementation that adds support for - * Jackson's {@code @JsonView} annotation declared on a Spring MVC - * {@code @HttpEntity} and {@code @RequestBody} method parameters. + * A {@link RequestBodyAdvice} implementation that adds support for Jackson's + * {@code @JsonView} annotation declared on a Spring MVC {@code @HttpEntity} + * or {@code @RequestBody} method parameter. * - *

The deserialization view specified in the annotation will be passed in to - * the {@code MappingJackson2HttpMessageConverter} which will then use it to - * deserialize the request body with. + *

The deserialization view specified in the annotation will be passed in to the + * {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter} + * which will then use it to deserialize the request body with. * *

Note that despite {@code @JsonView} allowing for more than one class to * be specified, the use for a request body advice is only supported with * exactly one class argument. Consider the use of a composite interface. * - *

Jackson 2.5.0 or later is required. + *

Jackson 2.5 or later is required for parameter-level use of {@code @JsonView}. * * @author Sebastien Deleuze * @since 4.2 + * @see com.fasterxml.jackson.annotation.JsonView * @see com.fasterxml.jackson.databind.ObjectMapper#readerWithView(Class) */ public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter { @@ -51,6 +52,7 @@ public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter { @Override public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { + return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) && methodParameter.getParameterAnnotation(JsonView.class) != null); } @@ -58,6 +60,7 @@ public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter { @Override public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class> selectedConverterType) throws IOException { + JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class); Class[] classes = annotation.value(); if (classes.length != 1) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java index 63fe943d3d..944706dbad 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -26,13 +26,13 @@ import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; /** - * A {@code ResponseBodyAdvice} implementation that adds support for - * Jackson's {@code @JsonView} annotation declared on a Spring MVC - * {@code @RequestMapping} or {@code @ExceptionHandler} method. + * A {@link ResponseBodyAdvice} implementation that adds support for Jackson's + * {@code @JsonView} annotation declared on a Spring MVC {@code @RequestMapping} + * or {@code @ExceptionHandler} method. * - *

The serialization view specified in the annotation will be passed in to - * the {@code MappingJackson2HttpMessageConverter} which will then use it to - * serialize the response body with. + *

The serialization view specified in the annotation will be passed in to the + * {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter} + * which will then use it to serialize the response body with. * *

Note that despite {@code @JsonView} allowing for more than one class to * be specified, the use for a response body advice is only supported with @@ -40,6 +40,7 @@ import org.springframework.http.server.ServerHttpResponse; * * @author Rossen Stoyanchev * @since 4.1 + * @see com.fasterxml.jackson.annotation.JsonView * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) */ public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/InfoReceiver.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/InfoReceiver.java index b039c4dcca..590f84c5ac 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/InfoReceiver.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/InfoReceiver.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.socket.sockjs.client; import java.net.URI; @@ -43,4 +44,4 @@ public interface InfoReceiver { */ String executeInfoRequest(URI infoUrl, HttpHeaders headers); -} \ No newline at end of file +} diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/Transport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/Transport.java index 840858d85f..f3705f6682 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/Transport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/Transport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.web.socket.sockjs.client; +package org.springframework.web.socket.sockjs.client; import java.util.List; @@ -40,7 +40,6 @@ public interface Transport { /** * Connect the transport. - * * @param request the transport request. * @param webSocketHandler the application handler to delegate lifecycle events to. * @return a future to indicate success or failure to connect. diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrTransport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrTransport.java index d5725ed54a..bcd7d35b3b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrTransport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/XhrTransport.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.socket.sockjs.client; import java.net.URI;