Question

I want to send GET-Requests which shall be answered by my REST-API. My java programm currently supports text/plain, text/html, text/xml and application/json using the JAX-RS Reference Implementation Jersey.

To test the different media types I'm using the firefox addon RESTClient. To change the media type I shall adjust the header with name=Content-Type and e.g. value=text/xml.

enter image description here

But the RESTClient always returns text/html no matter which Content-Type I choose. The only way right now to modify the returned result type is, to uncomment the html-section in my code. Then text/plain will be the returned media type, but still the Content-Type argument of the RESTClient stays ignored.

I'm using the most resent version of RESTClient, which is right now 2.0.3. Can you please help me?

Here is my 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);
  }

} 
Était-ce utile?

La solution

I think you have to specify the Accept header with the media type you want in addition to the Content-Type header that states what is the content type of your request, NOT the content type of the response which indeed is set by the Accept header

So use the Accept header instead of Content-Type header

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top