@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/