[英]How to change Content-Type of GET-Request via firefox addon RESTClient
我想发送GET-Requests,我的REST-API将回答这些请求。 我的java程序目前支持使用JAX-RS Reference Implementation Jersey的text/plain
, text/html
, text/xml
和application/json
。
要测试不同的媒体类型,我正在使用firefox插件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);
}
}
我认为你必须指定带有你想要的媒体类型的Accept
标头,除了Content-Type标头,它说明了你的请求的内容类型,而不是确实由Accept
标头设置的响应的内容类型。
因此,请使用Accept标头而不是Content-Type标头
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.