0

저는 REST 웹 서비스를 시도하고 있습니다. 표준 샘플; 전쟁을 만들고 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' : // localhost를 : 9100 /' – gigadot

+0

같은 302 오류; –

답변

0

(302)는 리디렉션 의미합니다. curl --verbose를 사용하여 끝점에 연결하고 Location : 헤더를 살펴보십시오. 그것은 당신에게 무슨 일이 일어나고 있는지에 대한 단서를 제공해야합니다.

0

좋아, 이제 내가 내 친구에게서 얻은 해결책이다. web.xml에 변화/나머지

->/휴식/*

전쟁 파일을 만들고 (내 경우 WSO2에) 서버에 업로드; 서버가 전쟁 파일을 업로드 할 때 , 그것은 (내 경우 RSTest에서) 문맥 함께 만들어

마지막으로 브라우저 종류로 이동 :

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

당신은 (이 경우 성공적인 결과를 볼 수 있습니다 HTTP`와 안녕하세요 뉴저지)

-Indu