Spring Integration: HTTP Inbound Gateway - using JSON to Object transformer for HashMap - spring

I am trying to setup a Spring Integration based Restful service. I am trying to avoid creating individual objects for request and response but instead use a HashMap for simple functions such as isUserAvailableForRegistration at service.
e.g
HashMap<String, String> isUserAvailableForRegistration(HashMap<String,String> request)
The following is the integration related context information:
<bean class="org.springframework.integration.http.inbound.UriPathHandlerMapping"/>
<int-http:inbound-gateway id="inboundGateway"
request-channel="incoming" reply-channel="outgoing"
supported-methods="POST" reply-timeout="5000"
path="/customer/isUserAvailable" />
<int:channel id="incoming" />
<int:channel id="outgoing" />
<int:service-activator id="activator" input-channel="incoming" ref="customerManagementGateway"
method="isUserAvailableForRegistration" output-channel="outgoing" requires-reply="true" />
<int:object-to-json-transformer id="outgoingJsonConverter"
input-channel="outgoing" />
<int:json-to-object-transformer id="incomingJsonConverter" input-channel="incoming" type="java.util.HashMap" />
While I can successfully test that I could marshall HashMap to JSON successfully, I am missing the configuration to unmarshall JSON data into HashMap.
I get the following exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.integration.dispatcher.AggregateMessageDeliveryException: All attempts to deliver Message to MessageHandlers failed. Multiple causes:
failed to transform message
org.springframework.expression.AccessException: Problem invoking method: public java.util.Map com.testapp.customer.CustomerManagementServiceActivator.isUserAvailableForRegistration(java.util.Map)
See below for the stacktrace of the first cause.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.springframework.integration.dispatcher.AggregateMessageDeliveryException: All attempts to deliver Message to MessageHandlers failed. Multiple causes:
failed to transform message
org.springframework.expression.AccessException: Problem invoking method: public java.util.Map com.testapp.customer.CustomerManagementServiceActivator.isUserAvailableForRegistration(java.util.Map)
See below for the stacktrace of the first cause.
org.springframework.integration.dispatcher.UnicastingDispatcher.handleExceptions(UnicastingDispatcher.java:164)
org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:127)
org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.springframework.integration.transformer.MessageTransformationException: failed to transform message
org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:44)
org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:67)
org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:97)
org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:114)
org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.ClassCastException: [B cannot be cast to java.lang.String
org.springframework.integration.json.JsonToObjectTransformer.transformPayload(JsonToObjectTransformer.java:30)
org.springframework.integration.transformer.AbstractPayloadTransformer.doTransform(AbstractPayloadTransformer.java:33)
org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)
org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:67)
org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:97)
org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:114)
org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Can anybody help me with figuring out what could possible be wrong ?
I have double checked the headers, hence i am sure that I it's something with the configuration or Jackson's support for unmarshalling to HashMap. Thanks for your time.

As Biju mentioned the integration graph wasnt correct, but the error was thrown even before the message reached json-to-object-transformer. After fixing the integration graph correctly i still was getting this error. after searching in spring integration forum, i found that mentioning request-payload-type in the int-http:inbound-gateway gateway solved the problem.
So the correct configuration is
<int-http:inbound-gateway id="inboundGateway"
request-channel="incoming" reply-channel="outgoing"
supported-methods="POST" reply-timeout="5000"
path="/customer/isUserAvailable" request-payload-type="String" />

Doesn't look like you have connected the different endpoints and channels correctly -
This is the flow that you have:
httpgateway->incoming(c)->customerManagementGateway->outgoing(c)->outgoingJsonConverter
->incomingJsonConverter
Both incomingJsonConverter and customerManagementGateway seem to be consuming off incoming channel
This may not be the main reason for the error message though, I think you need to convert the byte array to a String payload for the json transformer: try introducing this transformer also between your inputchannel and jsonconverter:
JsonToObjectTransformer:
<int:object-to-string-transformer input-channel="in" output-channel="out"/>

Related

Occasional NullPointerException when publishing STOMP message using SpringFramework

I am running into some occasional NPE when publishing STOMP messages using Spring (5.2.0).
Unfortunately I cannot precisely provide the steps to reproduce but I was hoping to get some insight into how this could happen.
I looked into the source and the only thing I can think of is that the channel is null. I'm hoping to get a confirmation and possible reason for this, or an alternate explanation.
The client did not disconnect but it seems like this error causes the client to disconnect as it receives a 1002 code.
Attached is the stacktrace. Any help or insight would be greatly appreciated. Thanks!
org.springframework.messaging.MessageDeliveryException: Failed to send message to ExecutorSubscribableChannel[clientInboundChannel]; nested exception is java.lang.NullPointerException
at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:146)
at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:122)
at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:302)
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:335)
at org.springframework.web.socket.handler.WebSocketHandlerDecorator.handleMessage(WebSocketHandlerDecorator.java:75)
at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.handleMessage(LoggingWebSocketHandlerDecorator.java:56)
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.handleMessage(ExceptionWebSocketHandlerDecorator.java:58)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:114)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:43)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:85)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:82)
at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395)
at org.apache.tomcat.websocket.server.WsFrameServer.sendMessageText(WsFrameServer.java:119)
at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495)
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294)
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:82)
at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171)
at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)
at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: null
at org.springframework.messaging.support.AbstractMessageChannel$ChannelInterceptorChain.applyPreSend(AbstractMessageChannel.java:178)
at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:132)
... 28 common frames omitted```
Not related to this problem; but you should try to keep up with maintenance releases; the current 5.2.x version is 5.2.6.
That NPE implies you have somehow ended up with a null in the interceptor list.
Line 178 in the abstract channel is invoking each interceptor in the list.
https://github.com/spring-projects/spring-framework/blob/927b8c15ef20eaaa4002d4b2170cc536a6d6aa35/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java#L178
I think the only way you can get an NPE there is if you have (or something has) added null to the list.
for (ChannelInterceptor interceptor : interceptors) {
Message<?> resolvedMessage = interceptor.preSend(messageToUse, channel); // line 178
if (resolvedMessage == null) {
...

NoSuchMethodError WebUtils.isValidOrigin Spring websocket

Here is my setup for a simple websocket controller in spring:
#Controller
#RequestMapping("/myHandler")
public class MyHandler extends TextWebSocketHandler {
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String reply = "Hello there!!!!!!!!";
session.sendMessage(new TextMessage(reply));
}
}
mcv-dispatcher-servlet.xml
<websocket:handlers allowed-origins="http://localhost">
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
<bean id="myHandler" class="com.aexp.socket.MyHandler"/>
I have updated all the jar files to correct versions
but I am getting following error:
HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/Websocket/myHandler; nested exception is java.lang.NoSuchMethodError: org.springframework.web.util.WebUtils.isValidOrigin(Lorg/springframework/http/HttpRequest;Ljava/util/Collection;)Z
root cause
org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/Websocket/myHandler; nested exception is java.lang.NoSuchMethodError: org.springframework.web.util.WebUtils.isValidOrigin(Lorg/springframework/http/HttpRequest;Ljava/util/Collection;)Z
org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest(WebSocketHttpRequestHandler.java:135)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
And when I am trying to create WebSocket object in JS I get following:
(Opcode -1)
This is the first time I am trying to use WebSockets so not sure what I am missing. Can someone point me to the right direction. Thanks.
Try modifying your .xml configuration as follows:
<websocket:handlers allowed-origins="http://localhost">
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
<websocket:sockjs/>
</websocket:handshake-interceptors>
Hope that helped!
You are missing the "spring-web" dependency.
Add the following to your pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
Also you probably need to run the "clean" goal, and rebuild the project.

spring content negotiating force Mime-type in error case

I ve got a spring service generating a pdf, if you call that service in the browser with .pdf path extension everything is working fine in success case.
However, in error case I would like to return a json error response as for all my other services.
Unfortuently, spring (Version 4.1.4) throws an exception org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Even if I add an accept-header stating that pdf or json might be ok.
I performed following tests:
1. Request Call myService/doc.pdf with standard browser accept header
Result: Exception
2. Request Call myService/doc.pdf with specific accept header application/pdf; application/json
Result: Exception
3. Request Call myService/doc with standard browser accept header
json is returned succesfully in case of error
4. Request Call myService/doc with accept header application/pdf;application/json
json is returned succesfully in case of error
It seems to me that in case path extension is used by Spring to determine response mime-types, ONLY that Mime Type is allowed in response. However, I think it does not make any sense, to return a pdf document in case of error.
Does anybody know if I may overrule content negotiation in that case in my exception handler. Saying, well I know, that mime-type pdf is requested in url path extension, however, I Must return mime-type json.
Thanks a lot for your support in advance.
--
Volker
My content negotiation config:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="false" />
<property name="defaultContentType" value="application/json"/>
<property name="mediaTypes" >
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
My ErrorHandler:
#ExceptionHandler(MyControlException.class)
#ResponseBody
public ResponseEntity<ErrorModel> handleMyExceptions(final MyControlException ex, final HttpServletRequest request,
final HttpServletResponse res) {
ResponseEntity<ErrorModel> response;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
response = new ResponseEntity<ErrorModel>(ex.getError(), headers, HttpStatus.valueOf(ex.getError().getErrorCode()));
return response;
}
Exception Stack Trace:
o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke #ExceptionHandler method: public org.springframework.http.ResponseEntity<com.myapp.apps2Control.common.model.ErrorModel> com.myapp.apps2Control.rest.controller.BaseApps2MceController.handleMyExceptions(com.myapp.apps2Control.common.MyControlException,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:144) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:362) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:60) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1167) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1004) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]

Custom HTTP Status Code other than Registered Status Code

I am trying to use a custom HTTP Status Code (450) but I get an error "org.springframework.http.HttpStatus doesn't have any enum constant '450' defined". I tried searching the other threads, but I wasn't able to find a suitable reply to my situation. Any help is greatly appreciated. Thanks!
Please find below my code:
<http:inbound-gateway supported-methods="POST"
request-channel="headerEnricher"
error-channel="errorHandlerRouterChannel"
reply-channel="responseChannel" header-mapper="headerMapper"
path="/testApi/1.0" request-payload-type="java.lang.String" />
Error handler method:
private Message<String> createErrorResponse() {
Map<String, String> respHeader = new HashMap<String, String>();
respHeader.put(HttpHeaders.STATUS_CODE, "450");
return MessageBuilder.withPayload("Validation Error")
.copyHeaders(respHeader).build();
}
Error Exception:
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: No matching constant for [450]
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No matching constant for [450]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
root cause: java.lang.IllegalArgumentException: No matching constant for [450]
org.springframework.http.HttpStatus.valueOf(HttpStatus.java:426)
org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.resolveHttpStatusFromHeaders(HttpRequestHandlingEndpointSupport.java:549)
org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.setupResponseAndConvertReply(HttpRequestHandlingEndpointSupport.java:443)
org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:103)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
a) I would argue that Spring is buggy when it only supports a hard-wired set of status codes; these are an extension point in HTTP and thus the set of status code is open-ended (well, until the range 100..599 has been filled up).
b) With respect to your use case -- maybe you could just use status code 422 instead (https://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422)?

Spring batch late binding for stepExecutionContext not working

I am using Spring batch application and want to use late binding for stepExecutionContext. I am facing issues in resolving my error.
Following is my reader which has sql property using late binding:
<bean id="itemReader_S4_JPolicy" class="com.aegonusa.etl.readers.JDBCItemReader" scope="step">
<property name="jobParameters" ref="jobParameters" />
<property name="dataSource" ref="readDataSource" />
<property name="rowMapper">
<bean class="com.aegonusa.etl.readers.ResultSetRowMapper" scope="step" />
</property>
<property name="sql"
value="#{stepExecutionContext['readQuery']}"></property>
</bean>
I set this property ('readQuery') in following class:
public class StepListener implements StepExecutionListener
{
....
....
#Override
public void beforeStep(StepExecution execution) {
String s = "select getdate();
StepSynchronizationManager.getContext().setAttribute("readQuery", s);
.....
}
.....
}
Please note that I am able to do late binding using jobParameters.
But using stepExecutionContext, I get following exc. I tried lot of posts in forums but am unable to resolve it:
Throwable occurred: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lazyBindingProxy.itemReader_S4_JPolicy#sysinit' defined in class path resource [Load_InforceToStage.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot bind to placeholder: stepExecutionContext['readQuery']
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:224)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:302)
at org.springframework.batch.core.scope.StepScope.get(StepScope.java:150)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.batch.core.scope.util.PlaceholderTargetSource.getTarget(PlaceholderTargetSource.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:184)
at $Proxy1.close(Unknown Source)
at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:83)
at org.springframework.batch.core.step.item.ChunkMonitor.close(ChunkMonitor.java:99)
at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:83)
at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:297)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:255)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:114)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:349)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
at com.ebsadm.lh.LHSHEDToStage.runJob(LHSHEDToStage.java:124)
at com.ebsadm.lh.LHSHEDToStage.main(LHSHEDToStage.java:111)
at com.ebdadm.lh.TestLHSHEDToStage.testInforceLoadToStage(TestLHSHEDToStage.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalStateException: Cannot bind to placeholder: stepExecutionContext['readQuery']
at org.springframework.batch.core.scope.util.PlaceholderTargetSource$1.convertIfNecessary(PlaceholderTargetSource.java:140)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1294)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
... 50 more
Kindly help.
I would expect a call to getExecutionContext on the StepExecution to give you the proper context.
#Override
public void beforeStep(StepExecution execution) {
String s = "select getdate()";
execution.getExecutionContext().putString("readQuery", s);
}
Your current solution is getting the StepContext. The StepContext is the object that is exposed and used for expression parsing, whereas the ExecutionContext associated with the StepExecution is the actual stepExecutionContext. ItemReaders and ItemWriters and such object also use the ExecutionContext to update the state (skipped items, commit count etc.).
Try any of the below :
Use promotionListener bean to promote the scope of variable from step to job.
Use bean instead of executionContext.

Resources