Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果存在不接受标头,则为默认媒体类型_Java_Xml_Json_Rest_Jersey - Fatal编程技术网

Java 如果存在不接受标头,则为默认媒体类型

Java 如果存在不接受标头,则为默认媒体类型,java,xml,json,rest,jersey,Java,Xml,Json,Rest,Jersey,在《泽西岛用户指南》中,我阅读了以下内容: @GET @Produces({"application/xml", "application/json"}) public String doGetAsXmlOrJson() { ... } If both are equally acceptable then the former will be chosen because it occurs first. 但是,在我的RESTful服务中(请注意,JSON媒体类型首先出现): 像这

在《泽西岛用户指南》中,我阅读了以下内容:

@GET
@Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
    ...
}

If both are equally acceptable then the former will be chosen because it occurs first.
但是,在我的RESTful服务中(请注意,JSON媒体类型首先出现):

像这样的请求:curl-v-xget
http://localhost:8080/myapp/users/2
,返回XML响应。如果请求指定接受头,如json或xml,则一切正常

编辑:

curl --trace - -X GET http://localhost:8080/myapp/users/2
== Info: Adding handle: conn: 0xc2ad68
== Info: Adding handle: send: 0
== Info: Adding handle: recv: 0
== Info: Curl_addHandleToPipeline: length: 1
== Info: - Conn 0 (0xc2ad68) send_pipe: 1, recv_pipe: 0
== Info: About to connect() to localhost port 8080 (#0)
== Info:   Trying 127.0.0.1...
== Info: Connected to localhost (127.0.0.1) port 8080 (#0)
=> Send header, 97 bytes (0x61)
GET /myapp/users/2 HTTP/1.1..User-Agent: curl/7.31.0..Hostlocalhost:8080..Accept: */*....
<= Recv header, 17 bytes (0x11)
HTTP/1.1 200 OK..
== Info: Server Apache-Coyote/1.1 is not blacklisted
<= Recv header, 27 bytes (0x1b)
Server: Apache-Coyote/1.1..
<= Recv header, 31 bytes (0x1f)
Content-Type: application/xml..
<= Recv header, 21 bytes (0x15)
Content-Length:234..
<= Recv header, 37 bytes (0x25)
Date: Sat, 07 Jun 2014 15:26:17GMT..
<= Recv header, 2 bytes (0x2)
..
<= Recv data, 234 bytes (0xea)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="2"><name>JOHN</name><surname>DOE</surname><heigth>172.5</heigth><weigth>70.5</weigth></user>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="2"><name>JOHN</name><surname>DOE</surname><heigth>172.5</heigth><weigth>70.5</weigth></user>
== Info: Connection #0 to host localhost left intact
curl--trace--X GEThttp://localhost:8080/myapp/users/2
==信息:添加句柄:连接:0xc2ad68
==信息:添加句柄:发送:0
==信息:添加句柄:recv:0
==Info:Curl\u addHandleToPipeline:length:1
==信息:-Conn 0(0xc2ad68)发送管道:1,接收管道:0
==信息:即将()连接到本地主机端口8080(#0)
==信息:正在尝试127.0.0.1。。。
==信息:连接到本地主机(127.0.0.1)端口8080(#0)
=>发送头,97字节(0x61)
GET/myapp/users/2 HTTP/1.1..User-Agent:curl/7.31.0..Hostlocalhost:8080..Accept://*。。。。

如果资源方法通过@products注释支持多种媒体类型,并且客户端接受多个媒体类型(例如,具有“Accept:*/*”标题),Jersey应首先返回列出的媒体类型。Jersey 2.x中存在一个错误,导致忽略媒体类型的顺序。见问题。该问题已得到修复,应在Jersey 2.16中发布该修复。

您可以使用命令:curl--traceIn my case Jersey browser发送带有“Accept:/”标题的飞行前选项请求吗。Jersey返回500和WADL信息…(*Chrome浏览器)我通过添加@OPTIONS方法返回Ok来解决。不优雅…该死。由于对Java6的依赖,我一直坚持使用Jersey 2.6。
curl --trace - -X GET http://localhost:8080/myapp/users/2
== Info: Adding handle: conn: 0xc2ad68
== Info: Adding handle: send: 0
== Info: Adding handle: recv: 0
== Info: Curl_addHandleToPipeline: length: 1
== Info: - Conn 0 (0xc2ad68) send_pipe: 1, recv_pipe: 0
== Info: About to connect() to localhost port 8080 (#0)
== Info:   Trying 127.0.0.1...
== Info: Connected to localhost (127.0.0.1) port 8080 (#0)
=> Send header, 97 bytes (0x61)
GET /myapp/users/2 HTTP/1.1..User-Agent: curl/7.31.0..Hostlocalhost:8080..Accept: */*....
<= Recv header, 17 bytes (0x11)
HTTP/1.1 200 OK..
== Info: Server Apache-Coyote/1.1 is not blacklisted
<= Recv header, 27 bytes (0x1b)
Server: Apache-Coyote/1.1..
<= Recv header, 31 bytes (0x1f)
Content-Type: application/xml..
<= Recv header, 21 bytes (0x15)
Content-Length:234..
<= Recv header, 37 bytes (0x25)
Date: Sat, 07 Jun 2014 15:26:17GMT..
<= Recv header, 2 bytes (0x2)
..
<= Recv data, 234 bytes (0xea)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="2"><name>JOHN</name><surname>DOE</surname><heigth>172.5</heigth><weigth>70.5</weigth></user>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="2"><name>JOHN</name><surname>DOE</surname><heigth>172.5</heigth><weigth>70.5</weigth></user>
== Info: Connection #0 to host localhost left intact