2013-07-31 10 views
5

Chcę wysłać Żądania GET, na które odpowie mój REST-API. Mój program java obsługuje obecnie text/plain, text/html, text/xml i application/json przy użyciu JAX-RS Reference Implementation Jersey.Jak zmienić typ treści żądania GET przez dodatek firefox RESTClient

Aby przetestować różne typy mediów, używam dodatku firefox RESTClient. Aby zmienić rodzaj materiału, dostosuję nagłówek za pomocą name=Content-Type i np. value=text/xml.

enter image description here

Ale RESTClient zawsze zwraca text/html niezależnie Content-Type wybiorę. Jedynym sposobem, aby zmodyfikować zwracany typ wyniku, jest odkomentowanie sekcji html w moim kodzie. Wtedy zwracany jest typ nośnika, ale nadal argument RESTClient pozostaje ignorowany.

Używam najbardziej resent wersji RESTClient, która jest teraz 2.0.3. Możesz mi pomóc?

Oto moja Java-Code:

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

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

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

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

    // This method is called if HTML is request 
    // Uncommenting the following 6 lines will result in returning text/plain 
    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public String sayHtmlHello() { 
    return "<html> " + "<title>" + "Hello World" + "</title>" 
     + "<body><h1>" + "Hello little World" + "</h1></body>" + "</html> "; 
    } 

    // This method is called if JSON is requested 
    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public String getJson(){ 
     Gson gsonObject = new Gson(); 
     return gsonObject.toJson(helloClass); 
    } 

} 

Odpowiedz

10

Chyba trzeba określić nagłówek z rodzaju nośnika chcesz oprócz nagłówka Content-Type, który stanowi, co jest typem zawartości Accept swoje prośba, nie jest typem treść odpowiedzi, które rzeczywiście jest wyznaczonym przez nagłówka

Accept więc używać Zebrane zamiast nagłówka Content-Type nagłówek

+0

dzięki! Dodałem nagłówek 'Accept: text/xml' i otrzymałem żądany format odpowiedzi. – Markus

+0

Witaj, jak dodać Accept: text/xml w dowolnych adresach URL GET – nilesh1212

Powiązane problemy