我试图以编程方式创建球衣资源(没有注释)。 我有一个方法raiseAlarm,它接受Name和id作为输入参数。我想从JSON输入中获取Name,并且我希望id来自path参数。代码看起来像这样......
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();
}
}
}
以上代码有效。 我想知道一种方式,我可以以编程方式声明路径参数或查询参数,以便我可以将我的方法签名定义为raiseAlarm(名称通知,字符串ID)并避免@PathParam(" id")注释
答案 0 :(得分:0)
我知道它刚才被问过,但我遇到了同样的问题并找到了答案。请参阅我的问题和自己的答案here以获取完整的代码。
您必须以与@Path
注释相同的方式添加带有路径的子资源。之后,您可以通过上下文的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
}
});