gpt4 book ai didi

jersey - 以编程方式在 Jersey 中声明路径参数

转载 作者:行者123 更新时间:2023-12-02 04:43:57 32 4
gpt4 key购买 nike

我正在尝试以编程方式创建 Jersey 资源(没有注释)。我有一个将 Name 和 id 作为输入参数的方法 raiseAlarm。我想从 JSON 输入中获取名称,并且我希望 id 来自路径参数。代码看起来像这样......

public class JerseyExample
{
public static void main(String[] args)
{
JerseyExample deployer = new JerseyExample();
deployer.init();
}

public static class BaseResource extends ResourceConfig
{
public BaseResource()
{
init();
}

public void init()
{
try
{
Resource.Builder resourceBuilder2 = Resource.builder();
resourceBuilder2.path("/raiseAlarm/{id}");
ResourceMethod.Builder method2 = resourceBuilder2.addMethod("POST")
.consumes(MediaType.APPLICATION_JSON_TYPE)
.produces(MediaType.APPLICATION_JSON_TYPE)
.handledBy(this,
this.getClass().getMethod("raiseAlarm", Name.class, String.class));

Resource childResource1 = resourceBuilder2.build();

Resource.Builder resourceBuilder = Resource.builder();
resourceBuilder.path("/employee/status");
resourceBuilder.addChildResource(childResource1);
Resource rootResource = resourceBuilder.build();
registerResources(rootResource);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public String raiseAlarm(Name notification, @PathParam("id") String id)
{
System.out.println("INSIDE RAISE ALARM ");
System.out.println(notification.toString() + " ID: " + id);
return "Result";
}

public void destroy()
{

}

public static class Name
{

String firstName;

String lastName;

String middleName;

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getMiddleName()
{
return middleName;
}

public void setMiddleName(String middleName)
{
this.middleName = middleName;
}

@Override
public String toString()
{
return firstName + " " + middleName + " " + lastName;
}
}
}

public void init()
{

Server server = new Server();

ServletContextHandler context0 =
new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder serveltHolder1 =
new ServletHolder(new ServletContainer(new BaseResource()));

context0.addServlet(serveltHolder1, "/*");
context0.setVirtualHosts(new String[] {"@external"});
ServerConnector connector = new ServerConnector(server);
connector.setHost("localhost");
connector.setPort(9069);
connector.setName("external");

HandlerCollection collection = new HandlerCollection();
collection.addHandler(context0);
server.setHandler(collection);
server.addConnector(connector);

try
{
server.start();
server.join();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

上面的代码有效。我想知道一种可以以编程方式声明 Path 参数或查询参数的方法,以便我可以将我的方法签名定义为 raiseAlarm(Name notification,String id) 并避免 @PathParam("id") 注释。

最佳答案

我知道前段时间有人问过这个问题,但我遇到了同样的问题并找到了答案。查看我的问题和自己的答案 here获取完整代码。

您必须以与 @Path 注释相同的方式添加具有路径 的子资源。之后可以通过context的getUriInfo()方法获取path参数。

像这样(假设您已经有一个 Resource.Builder 对象):

final Resource.Builder subResourceBuilder = resourceBuilder.addChildResource("{id}");

subResourceBuilder.addMethod("GET")
.produces(MediaType.APPLICATION_JSON_TYPE)
.handledBy(new Inflector<ContainerRequestContext, Response>() {

@Override
public Response apply(ContainerRequestContext rctx) {
// Get to the path parameter
MultivaluedMap<String, String> pparams = rctx.getUriInfo().getPathParameters();
List<String> idValues = pparams.get("id");
// Create response here
}
});

关于jersey - 以编程方式在 Jersey 中声明路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977651/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com