0

我只是嘗試REST webservice;標準樣品; 雖然我能夠創建戰爭並將其加載到WSO2 AS中,但當我嘗試將其與客戶端連接時,我得到了HTTP 302的響應;我想知道錯誤在哪裏。 以下是我的文件列表。REST Webservice - 示例代碼 - 提供HTTP 302響應

package com.indu.rs.test; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

// POJO, no interface no extends 

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types, 
// text, XML and HTML. 

// The browser requests per default the HTML MIME type. 

//Sets the path to base URL + /hello 
@Path("/hello") 
public class Hello { 

    // This method is called if TEXT_PLAIN is request 
    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String sayPlainTextHello() { 
     return "Hello Jersey"; 
    } 

    // This method is called if XML is request 
    @GET 
    @Produces(MediaType.TEXT_XML) 
    public String sayXMLHello() { 
     return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>"; 
    } 

    // This method is called if HTML is request 
    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public String sayHtmlHello() { 
     return "<html> " + "<title>" + "Hello Jersey" + "</title>" 
       + "<body><h1>" + "Hello Jersey" + "</h1></body>" + "</html> "; 
    } 

} // class 

的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.indu.rs.test</display-name> 

    <servlet> 
    <servlet-name>Jersey1</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.indu.rs.test</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>Jersey1</servlet-name> 
    <url-pattern>/rest</url-pattern> 
    </servlet-mapping> 
</web-app> 

客戶

package com.indu.rs.test.client; 

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 RsTest { 
    public static void main(String[] args) { 
     ClientConfig config = new DefaultClientConfig(); 
     Client client = Client.create(config); 
     WebResource service = client.resource(getBaseURI()); 

     // Fluent interfaces 
     System.out.println(service.path("rest").path("hello").accept(
       MediaType.TEXT_PLAIN).get(ClientResponse.class).toString()); 

     // Get plain text 
     System.out.println(service.path("rest").path("hello").accept(
       MediaType.TEXT_PLAIN).get(String.class)); 

     // Get XML 
     System.out.println(service.path("rest").path("hello").accept(
       MediaType.TEXT_XML).get(String.class)); 

     // The HTML 
     System.out.println(service.path("rest").path("hello").accept(
       MediaType.TEXT_HTML).get(String.class)); 


    } 

    private static URI getBaseURI() { 
     return UriBuilder.fromUri(
       "http://localhost:9100/com.indu.rs.test").build(); 
    } 

} 
+0

嘗試'getBaseURI'用'HTTP://本地主機:9100 /' – gigadot

+0

同樣顯示302錯誤; –

回答

0

302意味着你將被重定向。使用curl --verbose連接到端點,並查看Location:標題。它應該給你一個關於發生的事情的線索。

0

好的,這裏是我最終從我的一個朋友那裏得到的解決方案;

在web.xml 變化/休息

- >/REST/*

創建WAR文件並上傳到服務器(在我的情況下,WSO2); 當服務器上傳war文件,它與上下文創建它(在我的情況RSTest)

最後去的瀏覽器類型:

http://localhost:9100/RSTest/rest/hello 

,你會看到一個成功的結果(在這種情況下,你好州)

-Indu

相關問題