如何在我的 RestService 中创建 URL?
How to make a URL in my RestService?
我正在尝试创建 RestService,我想在其中像这样进行 URL 调用 -
http://localhost/pat/v1/clients/12345/data/flag
我将在 url 中将 clientId
作为 12345
传递,我还将通过 POSTMAN 将 JSON 文档传递到我的 RESTService。所以我需要提取 12345 的 clientId 和我发布到我的 REST 服务的 JSON 文档。
我有下面的代码,它使 URL 像现在这样 - http://localhost/pat/consumer
然后无论 JSON 我发布到我的服务,我都通过 InputStream 提取它.
@Path("/pat")
public class HelloWorldService {
@POST
@Path("consumer")
@Consumes(MediaType.APPLICATION_JSON)
public Response crunchREST(InputStream incomingData) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
System.out.println("Data Received: " + sb.toString());
}
}
我最近开始使用 RestService。如何在我的 RestService 中进行上述 URL 调用?并提取方法中的 clientId
以及 JSON 以及我使用 POSTMAN 发布的方法。
因为 id
是您 URL 中唯一变化的部分,所以将其用作 PathParam。
给你:
@Path("/v1/clients/{id}/data/flag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response crunchREST(InputStream incomingData,@PathParam("id") Integer id)
我正在尝试创建 RestService,我想在其中像这样进行 URL 调用 -
http://localhost/pat/v1/clients/12345/data/flag
我将在 url 中将 clientId
作为 12345
传递,我还将通过 POSTMAN 将 JSON 文档传递到我的 RESTService。所以我需要提取 12345 的 clientId 和我发布到我的 REST 服务的 JSON 文档。
我有下面的代码,它使 URL 像现在这样 - http://localhost/pat/consumer
然后无论 JSON 我发布到我的服务,我都通过 InputStream 提取它.
@Path("/pat")
public class HelloWorldService {
@POST
@Path("consumer")
@Consumes(MediaType.APPLICATION_JSON)
public Response crunchREST(InputStream incomingData) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
System.out.println("Data Received: " + sb.toString());
}
}
我最近开始使用 RestService。如何在我的 RestService 中进行上述 URL 调用?并提取方法中的 clientId
以及 JSON 以及我使用 POSTMAN 发布的方法。
因为 id
是您 URL 中唯一变化的部分,所以将其用作 PathParam。
给你:
@Path("/v1/clients/{id}/data/flag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response crunchREST(InputStream incomingData,@PathParam("id") Integer id)