[英]How to change Content-Type of GET-Request via firefox addon RESTClient
I want to send GET-Requests which shall be answered by my REST-API. 我想发送GET-Requests,我的REST-API将回答这些请求。 My java programm currently supports
text/plain
, text/html
, text/xml
and application/json
using the JAX-RS Reference Implementation Jersey. 我的java程序目前支持使用JAX-RS Reference Implementation Jersey的
text/plain
, text/html
, text/xml
和application/json
。
To test the different media types I'm using the firefox addon RESTClient
. 要测试不同的媒体类型,我正在使用firefox插件
RESTClient
。 To change the media type I shall adjust the header with name=Content-Type
and eg value=text/xml
. 要更改媒体类型,我将使用
name=Content-Type
调整标题,例如value=text/xml
。
But the RESTClient always returns text/html
no matter which Content-Type
I choose. 但无论我选择哪种
Content-Type
,RESTClient都会返回text/html
。 The only way right now to modify the returned result type is, to uncomment the html-section in my code. 现在修改返回结果类型的唯一方法是取消注释我的代码中的html-section。 Then
text/plain
will be the returned media type, but still the Content-Type
argument of the RESTClient stays ignored. 然后
text/plain
将是返回的媒体类型,但仍然忽略RESTClient的Content-Type
参数。
I'm using the most resent version of RESTClient, which is right now 2.0.3. 我正在使用RESTClient的最新版本,现在是2.0.3。 Can you please help me?
你能帮我么?
Here is my Java-Code: 这是我的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);
}
}
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 我认为你必须指定带有你想要的媒体类型的
Accept
标头,除了Content-Type标头,它说明了你的请求的内容类型,而不是确实由Accept
标头设置的响应的内容类型。
So use the Accept header instead of Content-Type header 因此,请使用Accept标头而不是Content-Type标头
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.