从main方法调用java应用程序中的Web服务

时间:2017-08-25 11:25:03

标签: java rest web-services web jersey

我正在使用Tom Cat服务器和Jersey Framework来实现RESTful Web服务。我有一个main方法,我想在main中调用它,并运行Tomcat。我怎么能这样做?

*ngFor

1 个答案:

答案 0 :(得分:0)

您需要开发一个REST客户端来调用REST服务。

在main()方法中定义这样的URL:

http://localhost:9080/"Context-root name"/"url-pattern"/"Path"/

localhost可以由运行应用程序的服务器的IP地址替换。 为运行应用程序的服务器提供正确的端口号。 找到context-Root名称并将其放在适当的位置。 然后检查web.xml中定义的URL模式,例如“/ rest / *”

<servlet-mapping> 
  <servlet-name>javax.ws.rs.core.Application</servlet-name> 
  <url-pattern>/rest/*</url-pattern> 
</servlet-mapping>

然后给出在写入REST服务的类中定义的X-Path。

通过这种方式可以从main()调用REST服务:

String inputForRest = "Test Message for REST";
RestClient client = new RestClient();
Resource resource = client.resource(restURL);
String responseStr = resource.contentType(MediaType.APPLICATION_XML).accept("*/*").post(String.class, inputForRest);

媒体类型可能会有所不同,具体取决于其他消费者,如JSON,XML等。