我尝试访问一个开放的数据Web服务,它为我提供了流量信息.文档说请求必须是GET并且需要包含Accept: application/json和Content-Type: application/json.我不明白为什么他们需要Content-Type但是确定:
我试图只用Accept:Header 检索数据,但我总是得到一个415 Unsupported Media Type.现在我正在尝试这种方式(但我不确定我是否真的正确设置两个标头):
String entity = ClientBuilder.newClient().target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(String.class);
Run Code Online (Sandbox Code Playgroud)
如你所见,我正在使用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);
Run Code Online (Sandbox Code Playgroud)