Java Code Examples for org.springframework.messaging.handler.annotation.Payload#required()

The following examples show how to use org.springframework.messaging.handler.annotation.Payload#required() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PayloadArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Payload ann = parameter.getParameterAnnotation(Payload.class);
	if (ann != null && StringUtils.hasText(ann.expression())) {
		throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
	}

	Object payload = message.getPayload();
	if (isEmptyPayload(payload)) {
		if (ann == null || ann.required()) {
			String paramName = getParameterName(parameter);
			BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
			bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty"));
			throw new MethodArgumentNotValidException(message, parameter, bindingResult);
		}
		else {
			return null;
		}
	}

	Class<?> targetClass = parameter.getParameterType();
	if (ClassUtils.isAssignable(targetClass, payload.getClass())) {
		validate(message, parameter, payload);
		return payload;
	}
	else {
		payload = (this.converter instanceof SmartMessageConverter ?
				((SmartMessageConverter) this.converter).fromMessage(message, targetClass, parameter) :
				this.converter.fromMessage(message, targetClass));
		if (payload == null) {
			throw new MessageConversionException(message,
					"No converter found to convert to " + targetClass + ", message=" + message);
		}
		validate(message, parameter, payload);
		return payload;
	}
}
 
Example 2
Source File: PayloadMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Payload ann = parameter.getParameterAnnotation(Payload.class);
	if (ann != null && StringUtils.hasText(ann.expression())) {
		throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
	}

	Object payload = message.getPayload();
	if (isEmptyPayload(payload)) {
		if (ann == null || ann.required()) {
			String paramName = getParameterName(parameter);
			BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
			bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty"));
			throw new MethodArgumentNotValidException(message, parameter, bindingResult);
		}
		else {
			return null;
		}
	}

	Class<?> targetClass = resolveTargetClass(parameter, message);
	Class<?> payloadClass = payload.getClass();
	if (ClassUtils.isAssignable(targetClass, payloadClass)) {
		validate(message, parameter, payload);
		return payload;
	}
	else {
		if (this.converter instanceof SmartMessageConverter) {
			SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
			payload = smartConverter.fromMessage(message, targetClass, parameter);
		}
		else {
			payload = this.converter.fromMessage(message, targetClass);
		}
		if (payload == null) {
			throw new MessageConversionException(message, "Cannot convert from [" +
					payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message);
		}
		validate(message, parameter, payload);
		return payload;
	}
}
 
Example 3
Source File: PayloadArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Payload ann = parameter.getParameterAnnotation(Payload.class);
	if (ann != null && StringUtils.hasText(ann.expression())) {
		throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
	}

	Object payload = message.getPayload();
	if (isEmptyPayload(payload)) {
		if (ann == null || ann.required()) {
			String paramName = getParameterName(parameter);
			BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
			bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty"));
			throw new MethodArgumentNotValidException(message, parameter, bindingResult);
		}
		else {
			return null;
		}
	}

	Class<?> targetClass = parameter.getParameterType();
	Class<?> payloadClass = payload.getClass();
	if (ClassUtils.isAssignable(targetClass, payloadClass)) {
		validate(message, parameter, payload);
		return payload;
	}
	else {
		if (this.converter instanceof SmartMessageConverter) {
			SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
			payload = smartConverter.fromMessage(message, targetClass, parameter);
		}
		else {
			payload = this.converter.fromMessage(message, targetClass);
		}
		if (payload == null) {
			throw new MessageConversionException(message, "Cannot convert from [" +
					payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message);
		}
		validate(message, parameter, payload);
		return payload;
	}
}
 
Example 4
Source File: SmartPayloadArgumentResolver.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message)
		throws Exception {
	Payload ann = parameter.getParameterAnnotation(Payload.class);
	if (ann != null && StringUtils.hasText(ann.expression())) {
		throw new IllegalStateException(
				"@Payload SpEL expressions not supported by this resolver");
	}

	Object payload = message.getPayload();
	if (isEmptyPayload(payload)) {
		if (ann == null || ann.required()) {
			String paramName = getParameterName(parameter);
			BindingResult bindingResult = new BeanPropertyBindingResult(payload,
					paramName);
			bindingResult.addError(
					new ObjectError(paramName, "Payload value must not be empty"));
			throw new MethodArgumentNotValidException(message, parameter,
					bindingResult);
		}
		else {
			return null;
		}
	}

	Class<?> targetClass = parameter.getParameterType();
	Class<?> payloadClass = payload.getClass();
	if (conversionNotRequired(payloadClass, targetClass)) {
		validate(message, parameter, payload);
		return payload;
	}
	else {
		if (this.messageConverter instanceof SmartMessageConverter) {
			SmartMessageConverter smartConverter = (SmartMessageConverter) this.messageConverter;
			payload = smartConverter.fromMessage(message, targetClass, parameter);
		}
		else {
			payload = this.messageConverter.fromMessage(message, targetClass);
		}
		if (payload == null) {
			throw new MessageConversionException(message,
					"Cannot convert from [" + payloadClass.getName() + "] to ["
							+ targetClass.getName() + "] for " + message);
		}
		validate(message, parameter, payload);
		return payload;
	}
}