Spring Integration Http休息服务

问题描述 投票:0回答:1

我试图在Spring集成中创建Rest API

@ResponseBody
    @RequestMapping(value="/employee", method=RequestMethod.GET,RequestMethod.POST},produces="application/json")
    public String getData(){        
        return "From Spring Rest";
    }

以下是我的文件样本在控制器中

@Service("employeeSearchService")
public class EmployeeSearchService {
    public String getEmployee(Message<?> inMessage){
        return "From Spring Integration RestFul API";
    }

在applicationContext-http-int.xml中

 <int:annotation-config/>
        <int:channel id="employeeSearchRequest" />
        <int:channel id="employeeSearchResponse" />
        <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"
            view-name="/employee" path="/services/employee"
            reply-timeout="50000">
        </int-http:inbound-gateway>
        <int:service-activator id="employeeServiceActivator"
                        input-channel="employeeSearchRequest" output-channel="employeeSearchResponse"
                        ref="employeeSearchService" method="getEmployee"
                        requires-reply="true" send-timeout="60000">
        </int:service-activator>

在web-application-config.xml中

<import resource="classpath:META-INF/spring/integration/applicationContext-http-int.xml"/>
    <context:component-scan base-package="org.springframework.integration.samples.rest"/>

在Web.xml中

<display-name>Spring Integration Rest HTTP Path Usage Demo</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            <!-- Spring application context declaration -->
            /WEB-INF/config/web-application-config.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>Spring Integration Rest HTTP Path Usage</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring Integration Rest HTTP Path Usage</servlet-name>
        <url-pattern></url-pattern>
    </servlet-mapping>

但是我得到以下错误。我从下面的URL下载此代码示例,我删除了一些代码。只有我需要REST API。任何人都可以帮助我。谢谢。 https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/rest-http

在DispatcherServlet中找不到具有URI [/ services / employee]的HTTP请求的映射,其名称为“Spring Integration Rest HTTP Path Usage”

@Gary Russell,等你回复。

java spring spring-integration
1个回答
1
投票

当您的sevlet没有读取您的应用程序上下文时,通常会发生此问题,因此不会在请求路径和业务逻辑之间构建任何绑定。

在servlet定义中,为contentConfigLocation设置正确的路径,与context-param中定义的路径相同。像这样的东西:

<servlet> 
    <servlet-name>Spring Integration Rest HTTP Path Usage</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/web-application-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
© www.soinside.com 2019 - 2024. All rights reserved.