Java Rest请求处理多个参数

Java Rest请求处理多个参数,java,rest,http,parameters,Java,Rest,Http,Parameters,我的问题是这样的: 这很难解释,因为很多论坛都在处理这类问题,但没有一个答案对我有用 我正在使用java,我想构建一个restful服务。我用的是: URI uri = new URIBuilder() .setScheme("http") .setHost("localhost") .setPort(8080) .setPath("/search")

我的问题是这样的: 这很难解释,因为很多论坛都在处理这类问题,但没有一个答案对我有用

我正在使用java,我想构建一个restful服务。我用的是:

URI uri = new URIBuilder()
                .setScheme("http")
                .setHost("localhost")
                .setPort(8080)
                .setPath("/search")
                .setParameter("first", "hello")
                .setParameter("second", "world,out")
                .setParameter("third", "there")
                .build();
我得到的URI为=

现在我想访问URI中的数据,如下所示:

@GET
@Path("search/")
@Produces(MediaType.TEXT_PLAIN)
public String test(@PathParam("first") String first, @PathParam("second") String second,@PathParam("third") String third) {
    return first+second+third;
}
但我得到的是:失败:HTTP错误代码:404

因此,我认为我对这个请求的处理程序不是它应该的方式。我尝试了很多不同的东西,但没有办法

我希望有人能向我解释如何以正确的方式访问数据。 谢谢你的帮助

编辑:这是我的控制器:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
          context.setContextPath("/");

          Server jettyServer = new Server(8081);
          jettyServer.setHandler(context);

          ServletHolder jerseyServlet = context.addServlet(
               org.glassfish.jersey.servlet.ServletContainer.class, "/*");
          jerseyServlet.setInitOrder(0);

          jerseyServlet.setInitParameter(
             "jersey.config.server.provider.classnames",
             RestfulFarmingGameService.class.getCanonicalName());

与jetty和jersey合作。这就是我所拥有的。

如果您使用的是
JAX-RS
,那么您也可以尝试这种方法

矩阵参数的概念是,它们是嵌入在矩阵中的任意一组名称-值对 uri路径段。矩阵参数示例如下:

GET http://host.com/library/book;name=EJB 3.0;author=Bill Burke
矩阵参数的基本思想是,它表示可通过其 属性及其原始id。
@MatrixParam
注释允许您插入URI矩阵 方法调用中的参数

 @GET
public String getBook(@MatrixParam("name") String name, @MatrixParam("author") String
author) {...}
当输入请求正文的类型为“application/x-www-form-urlencoded”时,也称为HTML 表单,您可以将请求主体中的各个表单参数注入方法参数 价值观


名字:

姓氏: @路径(“/resources/service”) @职位 public void addName(@FormParam(“firstname”)字符串优先, @FormParam(“lastname”)字符串 最后{…}
您使用的是@PathParam,因此您的参数必须位于url路径中,如下所示:

如果您想使用url参数发送,比如?first=somethin&second=something。。。依此类推,您必须使用@RequestParam而不是@PathVariable

@GET
@Path("search/")
@Produces(MediaType.TEXT_PLAIN)
public String test(@RequestParam("first") String first, @RequestParam("second") String second,@RequestParam("third") String third) {
    return first+second+third;
}

您是否设置了任何
contextPath
或任何带有
@Controller
注释的路径?PathParam用于。。。路径参数(
/search/{first}/{second}/{third}
)。你想要QueryParam。
@GET
@Path("search/")
@Produces(MediaType.TEXT_PLAIN)
public String test(@RequestParam("first") String first, @RequestParam("second") String second,@RequestParam("third") String third) {
    return first+second+third;
}