Announcement Announcement Module
Collapse
No announcement yet.
How can I explicitly set an HTTP response code? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I explicitly set an HTTP response code?

    Hello,

    I have a pretty basic HTTP message flow set up and am looking for a way to modify the response code when an exception is thrown (customer needs this for some reason...). After hitting the gateway, the request is sent to a handler where an exception is thrown while trying to save to the DB. The default behavior is to immediately send a 500 back. How can I modify the response to be a 400 instead? I have tried to include as many of the definitions as I though might be needed. Please let me know if you need more information. Thanks in advance.

    1. My web.xml file contains the entry point

    Code:
      <servlet>
        <servlet-name>inboundExampleGateway</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>inboundExampleGateway</servlet-name>
        <url-pattern>/exampleHttp</url-pattern>
      </servlet-mapping>
    2. Spring integration and bean definitions
    Code:
      <integration:channel id="exampleRequestChannel"/>
      <integration:channel id="exampleTerminatorChannel"/>
    
    
    
      <http:inbound-gateway id="inboundExampleGateway"
                                       name="inboundExampleGateway"
                                       request-channel="exampleRequestChannel"/>
    
    
      <integration:service-activator input-channel="exampleRequestChannel" 
                                               output-channel="exampleHeaderEnricherChannel"
        				           ref="exampleTransportHandler" method="exampleMethod"/>
    
    
      <bean id="exampleTransportHandler"
            class="com.example.application.ExampleSpringTransportHandler">
      </bean>
    
    
      <integration:header-enricher input-channel="exampleHeaderEnricherChannel"
                                             output-channel="exampleRouterChannel">
             <integration:header name="MessageDetails" expression="payload"/>
      </integration:header-enricher>
    
    
      <integration:router input-channel="exampleRouterChannel"
                          ref="exampleRouter"
                          method="exampleRouterMethod"/>
    
      <bean id="exampleRouter"
            class="com.example.application.MappedExpressionValueRouter">
        <property name="expression" value="['MessageDetails'].type()"/>
        <property name="exampleMap">
          <map>
            <entry key="FIRST" value="exampleTerminatorChannel"/>
            <entry key="SECOND" value="exampleTransformChannel"/>
          </map>
        </property>
      </bean>
    
      <integration:service-activator input-channel="exampleTerminatorChannel"
                                     ref="exampleEndpointHandler"
                                     method="terminate"/>
    
    
      <bean id="exampleEndpointHandler"
            class="com.example.application.TerminalEndpointHandler"/>
    3. Java code

    Code:
    public class ExampleSpringTransportHandler {
    
        public Object exampleMethod(Object payload, Map headers) {
    	...
    	...
    
    	Details deails = new Details();
    	saveToDB(details);
    
    	...
    	...
    
            } catch (Exception e) {
                    throw new MoreSpecificException(e);
            }
            return details;
        }
    
        public void saveToDB(Object obj){
    
    	...
            throw new Exception();
        }
    }
Working...
X