我想发送GET-Requests,它将由我的REST-API应答。我的java程序员目前使用JAX-RS参考实现Jersey支持text/plain、text/html、text/xml和application/json。
为了测试不同的媒体类型,我使用了火狐插件RESTClient。为了改变媒体类型,我应该用name=Content-Type和value=text/xml来调整报头。

但是无论我选择哪种Content-Type,RESTClient总是返回text/html。现在修改返回结果类型的唯一方法是,在我的代码中取消对html-section的注释。那么text/plain将是返回的媒体类型,但RESTClient的Content-Type参数仍然被忽略。
我使用的是最新版本的RESTClient,现在是2.0.3。你能帮帮我吗?
下面是我的Java代码:
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);
}
} 发布于 2013-08-03 00:56:07
我认为除了声明请求的内容类型的content -Type头之外,还必须指定具有所需媒体类型的Accept头,而不是由Accept头设置的响应的内容类型
因此,请使用Accept标头而不是Content-Type标头
https://stackoverflow.com/questions/17964088
复制相似问题