gpt4 book ai didi

java - 更改 rest API 的 URL 模式

转载 作者:行者123 更新时间:2023-11-30 06:52:05 25 4
gpt4 key购买 nike

@Path("/ftocservice")
public class RestService {

@Path("{f}")
@GET
@Produces("application/json")

public Response convertFtoCfromInput(@PathParam("f") float f)
throws Exception {

DbCon db = new DbCon();
ArrayList<Student> students = db.getStudentList();
JSONArray jsonArray = new JSONArray(students);

String result = jsonArray.toString();

return Response.status(200).entity(result).build();
}

}

我正在使用上面的源代码生成 rest API 并且用户通过 API 请求如下

http://localhost:8080/RestExample/RestService/ftocservice/23

我需要按如下方式更改请求 URL。

http://localhost:8080/RestExample/RestService/ftocservice?f=23

请帮助更改源代码以更改给定的请求 URL。谢谢

最佳答案

改为使用@QueryParam:

@Path("/ftocservice")
public class RestService {


@GET
@Produces("application/json")
public Response convertFtoCfromInput(@QueryParam("f") float f)
throws Exception {

DbCon db = new DbCon();
ArrayList<Student> students = db.getStudentList();
JSONArray jsonArray = new JSONArray(students);

String result = jsonArray.toString();

return Response.status(200).entity(result).build();
}
}

查看此 link有关 JAX-RS 中参数类型的更多信息。

tutorial通过 Mkyong.com也挺好的。

关于java - 更改 rest API 的 URL 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39568494/

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