【发布时间】:2013-09-06 21:07:27
【问题描述】:
我尝试访问提供交通信息的开放数据网络服务。文档说请求必须是GET 并且需要包含Accept: application/json 和Content-Type: application/json。我不明白他们为什么需要Content-Type 但没关系:
我尝试仅使用 Accept: 标头检索数据,但我总是得到 415 Unsupported Media Type。现在我正在尝试这种方式(但我不确定我是否真的正确设置了两个标题):
String entity = ClientBuilder.newClient().target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(String.class);
如您所见,我使用的是 Jersey 2.2,但我仍然收到 415 Unsupported Media Type。
编辑
所以我让它工作了,但我不明白为什么。 accept(MediaType.APPLICATION_JSON)和header("Content-type","application/json")不一样吗?
String responseEntity = ClientBuilder.newClient()
.target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.header("Content-type", "application/json")
.get(String.class);
【问题讨论】:
标签: web-services rest jersey jax-rs content-type