I am trying to setup an http:inbound-gateway
that only accepts json. My xml config looks like.
<int-http:inbound-gateway id="inboundGateway"
supported-methods="POST"
request-payload-type="eu.model.MyRequest"
request-channel="inputChannel"
mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
path="/myService"
reply-timeout="50000"
message-converters="converters"
merge-with-default-converters="false"
validator="myValidator">
<int-http:request-mapping consumes="application/json"/>
</int-http:inbound-gateway>
<util:list id="converters">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</util:list>
I have seen that since 5.2 version a Validator can be used to check a payload before sending into the channel, but I cannot seem to find an example. Adding validator="myValidator"
seems to validate myRequest.
However although <int-http:request-mapping consumes="application/json"/>
restricts the content to valid json and Validator issues HTTP Status 400 – Bad Request
the error is returned in html?
How can this be overriden to return a custom json response ?
EDIT 1 This is my full xml configuration
<int-http:inbound-gateway id="inboundGateway"
supported-methods="POST"
request-payload-type="eu.neurocom.wind.msdp.cis.model.MyRequest"
request-channel="inputChannel"
reply-channel="responseChannel"
error-channel="errorChannel"
mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
path="/myservice"
reply-timeout="50000"
message-converters="converters"
merge-with-default-converters="false"
validator="myValidator">
<int-http:request-mapping consumes="application/json" produces="application/json" />
</int-http:inbound-gateway>
<util:list id="converters">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</util:list>
<int:service-activator
input-channel="errorChannel"
output-channel="responseChannel"
ref="globalExceptionHandler"
method="handleError"
/>
<int:service-activator ref="incomingActivator" input-channel="inputChannel" output-channel="responseChannel" method="handle"></int:service-activator>
My endpoint activator method looks like
public Message<MyResponse> handle(Message<MyRequest> message) {
logger.info("Received {}", message);
...}
If an error is thrown within the activator the error channel returns my custom json response otherwise before even logging the message I get below reponse in text/html;.
<body>
<h1>HTTP Status 400 – Bad Request</h1>
<hr class="line" />
<p><b>Type</b> Status Report</p>
<p><b>Message</b> Validation failure</p>
<p><b>Description</b> The server cannot or will not process the request due to something that is perceived to be a
client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
</p>
<hr class="line" />
<h3>Apache Tomcat/8.5.58</h3>
EDIT 2 Bellow is my validator class the looger is called and if msidsn is not present triggers
public class MyRequestValidator implements Validator {
private static final Logger logger = LoggerFactory.getLogger(MyRequestValidator.class);
@Override
public boolean supports(Class<?> clazz) {
return CisRequest.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
logger.debug("validatorCalled");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "msisdn", "msisdn.required");
}
}
My web.xml
<servlet>
<servlet-name>inboundGateway</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>\WEB-INF\classes\spring-integration-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->
<servlet-mapping>
<servlet-name>inboundGateway</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The logic in that component is like this:
message = prepareRequestMessage(servletRequest, httpEntity, headers, payload);
}
catch (Exception ex) {
MessageConversionException conversionException =
new MessageConversionException("Cannot create request message", ex);
MessageChannel errorChannel = getErrorChannel();
if (errorChannel != null) {
ErrorMessage errorMessage = buildErrorMessage(null, conversionException);
if (expectReply) {
return this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
}
else {
this.messagingTemplate.send(errorChannel, errorMessage);
return null;
}
}
else {
throw conversionException;
}
}
So, if you have configured an error-channel
for the <int-http:inbound-gateway>
, you can handle a thrown IntegrationWebExchangeBindException
and produce a custom reply as you wish.
NOTE: we probably need to mention such an approach in docs. At the moment it only points to standard Spring MVC way to handle validation errors: https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/http.html#http-validation. Feel free to raise a GH issue!