簡體   English   中英

更改其余API的URL模式

[英]Change URL pattern of rest API

@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();
  }
}

請參閱此鏈接以獲取有關JAX-RS中參數類型的更多信息。

Mkyong.com的教程也很不錯。

使用@QueryParam代替@PathParam

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM