如果我使用Jersey 1.12,并且我有多个资源类,并且它们都需要访问一些共享上下文,那么注入依赖项的最佳方法是什么,无论是在资源类的构造函数中还是在处理程序方法中?我是否需要使用外部DI库,或者Jersey内置了什么?
也许Foos的资源看起来像这样:
package com.example.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
@Path("/some/api/path/foo")
public class FooResource
{
@GET
@Produces("text/html")
public String getFoo(@QueryParam("id") String id)
{
Foo foo = /* get a Foo from some shared context based on id */
/* Process foo into a String */
}
}
Run Code Online (Sandbox Code Playgroud)
和酒吧:
package com.example.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
@Path("/some/api/path/bar")
public class BarResource
{
@GET
@Produces("text/html")
public String getBar(@QueryParam("id") String id)
{
Bar bar = /* get a Bar from some shared …Run Code Online (Sandbox Code Playgroud) 这是我的Java代码:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("x = " + x);
System.out.println("y = " + y);
return (x + y) + "\n";
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它:
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'
Run Code Online (Sandbox Code Playgroud)
问题是System.out.println呼叫一直张贴零零,看来我没有正确传递 x 和 y。
得到答复后,我将请求更改为:
curl -d '{"x" : 4, "y":3}' "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -H "Content-Type:application/json" -H "Accept:text/plain" --include
Run Code Online (Sandbox Code Playgroud)
服务是:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("sumPost");
System.out.println("x …Run Code Online (Sandbox Code Playgroud) 在restlet中寻找guice支持我遇到了这篇文章 - http://wiki.restlet.org/developers/172-restlet/257-restlet/284-restlet.html
但是 2.0(稳定版)和 2.1(候选发布版)都不包含 org.restlet.ext.guice.jar
所以,我的问题是它的状态是什么?我在哪里可以下载它?我正在使用restlet 2.0
谢谢。