我正在尝试使用Feign为我的网络服务构建一个REST客户端。 Web服务是使用Spring 4构建的,具有xml bean配置。
该项目使用Maven构建,并使用子模块构建
foo-api
--- foo-api-client
------ src/main/java/foo/client
--------- FooClientFactory.java
------ pom.xml
--- foo-api-shared
------ src/main/java/foo/shared
--------- FooClient.java
------ pom.xml
--- foo-api-service
------ src/main
--------- /java/foo/service
------------ /config
--------------- FeignConfiguration.java
------------ /controller
--------------- FooController.java
--------- /webapp/WEB-INF
------------ spring.xml
------------ web.xml
--- pom.xml
为了启用Feign客户端,我在Spring xml配置上创建了一个带注释的类。
spring.xml
...
<context:component-scan base-package="foo.service"/>
<context:annotation-config/>
<bean class="foo.service.config.FeignConfiguration" />
...
FeignConfiguration.java
package foo.service.config;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
public class FeignConfiguration {
}
然后我创建了一个Feign客户端并使用注释进行配置
FooClient.java
package foo.shared;
import feign.Headers;
import feign.RequestLine;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("foo")
public interface FooClient {
@RequestLine("GET /foo/v2/{id}")
@Headers("Accept: " + MediaType.APPLICATION_JSON_VALUE)
Object get(@PathVariable("id") String id);
}
API控制器按如下方式实现Feign客户端
FooController.java
package foo.service.controller;
@Controller
@RequestMapping("/foo")
public class FooController implements FooClient {
@Override
@RequestMapping(value = "/v2/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object get(@PathVariable("id") String id) {
...
}
...
}
foo-api-client模块jar用作外部客户端与foo-api-service REST服务联系的依赖项。为了允许这些客户端轻松使用api,已经创建了一个工厂类来生成FooClient的实例。
FooClientFactory.java
package foo.client;
import foo.shared.FooClient;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@Service
public class FooClientFactory {
@Autowired
private Environment env;
public static final String SERVER_URL_PROPERTY = "foo.api.url";
public FooClient build() {
return Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.target(FooClient.class, env.getProperty(SERVER_URL_PROPERTY));
}
}
问题
当外部客户端使用FooClientFactory fooClientFactory.build().get("id");
向foo Web服务执行请求时,将返回405错误。
以下是客户端控制台上的响应日志:
ERROR [http-nio-8091-exec-1] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException: status 405 reading FooClient#get(String); content:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 405 Request method 'POST' not supported</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /foo/v2/{id}. Reason:
<pre> Request method 'POST' not supported</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.7.v20160115</a><hr/>
</body>
</html>
] with root cause
feign.FeignException: status 405 reading FooClient#getOrder(String); content:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 405 Request method 'POST' not supported</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /foo/v2/{id}. Reason:
<pre> Request method 'POST' not supported</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.7.v20160115</a><hr/>
</body>
</html>
我在stackoverflow和其他博客周围搜索过这类问题,但我无法理解整个设置有什么问题。
有什么想法吗?
谢谢, 安德烈
答案 0 :(得分:0)
我在路径中看到两个“ foo”。
@FeignClient("foo")
和@RequestLine("GET /foo/v2/{id}")
将路径设置为/foo/foo/v2/{id}
,但是您的呼叫应该到/foo/v2/{id}
。
能否请您删除“ / foo”