簡體   English   中英

部署和運行war文件

[英]Deploying and running a war file

(請參閱下面的代碼代碼)我可以在eclipse環境中在Tomcat上運行它,它可以正常工作。 我已將以下內容導出到war文件並創建了Manifest.MF:

Manifest-Version: 1.0
Main-Class: com.process.Test

當代碼在Eclipse中運行時,來自服務器端的響應將輸出到控制台。

最后我的問題(原諒我的無知,我對此很新):

在我的Tomcat服務器上部署戰爭后,如何發送REST請求或運行war並顯示服務器響應?

什么是相當於:http:// localhost:8080 / rest / xml / list在實時服務器上?

在web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.process.Test</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.process.Test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

客戶端代碼:

import java.net.URI; 
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
//import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Test 
{       
public static void main(String[] args) {

    //Instead on using Apache Client 
    //used default Jersey client 
    //to send requests          
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);      
    String _package = "api.amebatv.com";
    WebResource service = client.resource(getBaseURI(_package));

    //runRequest(service,"indexpath");
    runRequest(service,"list");
}

/*
 * For testing purposes the base URI is :http://localhost:8080/package name
 * Will change to domain name for production code
 */
private static URI getBaseURI(String _package){
    return UriBuilder.fromUri(
            "http://localhost:8080/api.process.com").build();
}


private static void runRequest(WebResource service,String path){
    String response = service.path("rest/xml/"+path).accept(MediaType.APPLICATION_XML).get(String.class);
    System.out.println("Post Response :"+response);
}

}

服務器端:

@Path("/xml")

public class Service {

// List of video objects
private ArrayList<Video> videolist; 
//Parser object, parses xml  into video objects
private Parser parser = new Parser();


/*
 * Constructor
 * Parse XML and create video list on call
 */
public Service(){
    parser.createXML();
    videolist = parser.getList();       
}

/************************************************
 *GET
 * Path: /indexpath
 * list of all only <video> items
 * @return : ArrayList of Video objects
 ***********************************************/
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_XML)
public List<Video> getCustomerInXML() 
{ 
    return videolist;
}
}

Web應用程序的URL取決於許多因素,即應用程序服務器,配置文件,web.xml和特定於應用程序服務器的配置。

但在某些情況下,這是“如果你不知道的話,首先要嘗試”(不是官方的,但是“它有效”的經驗法則)

http://hostnameOrIP:8080/WARFILENAMEWITHOUTEXTENSION
  • hostnameOrIP :嗯,只需替換為您的主機名或IP
  • 8080替換為應用服務器的默認端口(9001,9090,3000等...)
  • WARFILENAMEWITHOUTEXTENSION替換為war文件名,而不是擴展名

如果你沒有其他方法可以告訴我,這又是一種非官方的,先試試的方式......

暫無
暫無

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

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