我希望使用RestEasy框架的仅接口选项,因为它更干净并且应该可以工作。
但是我在 POST 请求中传递参数时遇到问题。
我在文档中找到了这个例子:
@PUT
@Path("basic")
@Consumes("text/plain")
void putBasic(String body);
并调用:
import org.jboss.resteasy.client.ProxyFactory;
// ...
// this initialization only needs to be done once per VM
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081");
client.putBasic("hello world");
我尝试了以下方法:
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Path("http://localhost:8080/app/resource")
String postBasic(String body);
并调用:
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
RepoClient client = ProxyFactory.create(RepoClient.class, "");
client.postBasic("hi");
在被调用的 servlet 的 doPost
方法上打印参数 Map 时(并对其进行调试),参数为空。我真的看不出我的方法和记录的方法之间的区别(来自这里:Resteasy 界面示例)。
所以总结一下,只使用接口声明和代理实现如何发送 POST 参数?
解决方案:正如预期的那样...只需要使用收到的参数相应地声明消耗,它就可以工作了......问题是在另一个 servlet 中调用 servlet 的 POST 方法。
在 POST 示例中,@Path
不能包含绝对 URL。尝试只放/app
或/app/resource
,具体取决于您的配置。
正如怀疑者所说,@Path应该是一个相对的网址,我只对泽西岛有经验,我对 Resteasy 并不熟悉,但我认为这是一样的。
您的类将具有@Path注释,并且其中的方法可以具有@Path注释。
所以如果你有这样的东西:
@POST
@Path( "Foo" )
public class Foo()
{
@POST
@Path( "Bar" )
public String Bar()
{
...
}
}
因此,POST to http://localhost:8080/Foo/Bar 将执行方法 Bar。
我的评论越来越长,所以我就在这里加油。
抱歉,直到之后我才看到您对怀疑者的评论。 您要关闭的示例是否使用@FormParam?
鉴于我是 REST 的新手,但到目前为止,每个@POST方法都必须使用 @PathParam 或 @FormParam 您的方法如下所示:
@Post
@Path( "Foo/{foobar}" )
public String Bar(@PathParam( "foobar" ) String foobar)
{
}
或喜欢
@Post
@Path( "Foo" )
public String Bar(@FormParam( "foobar" ) String foobar)
{
}