春云网飞假装 - 错误 405 请求方法'POST'不支持



我正试图使用Feign为我的web服务构建一个REST客户端。web服务是使用Spring4构建的,具有xmlbeans配置。

该项目使用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客户端模块jar被外部客户端用作联系foo-api服务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 &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和其他博客上搜索了这类问题,但我一直无法理解整个设置的问题所在。

知道吗?

谢谢,Andrea

我在路径中看到两个"foo"。

CCD_ 2和CCD_ 3

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

你能试着在"/foo"上删除吗

相关内容

  • 没有找到相关文章

最新更新