gpt4 book ai didi

java - Spring Integration Http 休息服务

转载 作者:行者123 更新时间:2023-12-02 11:47:33 25 4
gpt4 key购买 nike

我正在尝试在 Spring 集成中创建 Rest API,例如

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

下面是我的文件示例在 Controller

@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]、名称为“Spring Integration Rest HTTP PathUsage”的 HTTP 请求的映射

@Gary Russell,等待您的回复。

最佳答案

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

在您的 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>

关于java - Spring Integration Http 休息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com