2

我正在尝试使用 Feign 为我的网络服务构建一个 REST 客户端。Web 服务是使用 Spring 4 构建的,具有 xml beans 配置。

该项目使用 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 配置上启用的带注释的类。

春天.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 对 foo web-service 执行请求时,fooClientFactory.build().get("id");会返回 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 &apos;POST&apos; not supported</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /foo/v2/{id}. Reason:
<pre>    Request method &apos;POST&apos; 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 &apos;POST&apos; not supported</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /foo/v2/{id}. Reason:
<pre>    Request method &apos;POST&apos; not supported</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.7.v20160115</a><hr/>

</body>
</html>

我在 stackoverflow 和其他博客上搜索了此类问题,但我无法理解整个设置有什么问题。

任何想法?

谢谢,安德里亚

4

1 回答 1

0

我在路径中看到两个“foo”。

@FeignClient("foo")@RequestLine("GET /foo/v2/{id}")

这使得路径为/foo/foo/v2/{id},但是您的呼叫应该是/foo/v2/{id}

您可以尝试删除“/ foo”吗

于 2019-08-31T13:48:37.723 回答