Spring Integration构建REST API遇请求映射错误求助
Hey there, let's break down why you're seeing that No mapping found for HTTP request with URI [/services/employee] error and get your REST API working to match that @ResponseBody + @RequestMapping behavior you expect from Spring MVC.
Root Cause
The most immediate issue is that your DispatcherServlet in web.xml has an empty url-pattern. Without a valid pattern, the servlet container has no idea to send your /services/employee request to Spring's DispatcherServlet—so it never finds the inbound gateway you defined in Spring Integration.
Step-by-Step Fixes
1. Fix the DispatcherServlet URL Mapping
Update your servlet-mapping in web.xml to include a pattern that matches your API path. Since your inbound gateway uses /services/employee, use a pattern that catches all requests under /services/:
<servlet-mapping> <servlet-name>Spring Integration Rest HTTP Path Usage</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
This tells the container to route all /services/ requests to Spring's DispatcherServlet, which will then hand them off to your Spring Integration inbound gateway.
2. Remove Unnecessary view-name Configuration
Your goal is to return a JSON response (like @ResponseBody), but the view-name="/employee" on your inbound gateway is meant for rendering server-side views (like JSPs). This will conflict with your REST response, so remove that attribute entirely:
<int-http:inbound-gateway id="inboundEmployeeSearchRequestGateway" supported-methods="GET, POST" request-channel="employeeSearchRequest" reply-channel="employeeSearchResponse" mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS" path="/services/employee" reply-timeout="50000"> </int-http:inbound-gateway>
3. Configure JSON Message Conversion (Match produces="application/json")
To get your response serialized to JSON, you need to add a message converter that handles JSON. Here are two ways to do this:
Option 1: Add Converter Directly to Inbound Gateway
First, define a Jackson converter bean in your applicationContext-http-int.xml:
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> </bean>
Then reference it in your inbound gateway:
<int-http:inbound-gateway id="inboundEmployeeSearchRequestGateway" supported-methods="GET, POST" request-channel="employeeSearchRequest" reply-channel="employeeSearchResponse" mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS" path="/services/employee" reply-timeout="50000" message-converters="jsonConverter"> </int-http:inbound-gateway>
Option 2: Global MVC Configuration (If Using Spring MVC)
If you already have Spring MVC set up, you can add the converter globally so all components use it:
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
Spring Integration's inbound gateway will automatically pick up this global converter.
4. Verify Your Service's Return Value
Right now, your EmployeeSearchService returns a string. With the JSON converter in place, this will be serialized as a quoted JSON string. If you want to return a full employee object later, just return your Employee class instance—the converter will handle turning it into proper JSON automatically.
Quick Checks to Ensure Everything Works
- Double-check that your
component-scaninweb-application-config.xmlcovers the package whereEmployeeSearchServicelives (org.springframework.integration.samples.rest). - Confirm all your Spring config files are being loaded correctly (no typos in paths or import statements).
内容的提问来源于stack exchange,提问作者Doss

