gpt4 book ai didi

json - Jersey 和 JSON

转载 作者:行者123 更新时间:2023-12-04 05:49:03 25 4
gpt4 key购买 nike

我已经使用 jersey 在 JAVA 中构建了一个 REST Web 服务。
一切正常,直到我将 MediaType 从 application_xml 切换到 application_json。

如果我使用 XML,那么一切都会正常工作:

客户端:

public static CoResponse rO = new CoResponse();

rO = service.path("check-in").accept(MediaType.APPLICATION_XML).put(CoResponse.class, rO);

服务器端:
@PUT
@Consumes(MediaType.APPLICATION_XML)
public CoResponse newCheckin(JAXBElement<CoResponse> obj){
CoResponse newObj = obj.getValue();
//DO SOMETHING....
return newObj
}

当我将 MediaType 更改为 Application_JSON 时,我收到 415 unsupported type 错误:

客户端:
public static CoResponse rO = new CoResponse();
rO = service.path("check-in").accept(MediaType.APPLICATION_JSON).put(CoResponse.class, rO);

服务器端:
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public CoResponse newCheckin(JAXBElement<CoResponse> obj){
CoResponse newObj = obj.getValue();
//DO SOMETHING....
return newObj
}

使用 JSON 时还有其他事情要做吗?
谢谢你。

我使用的类如下:

响应对象:
@XmlRootElement
public class CoResponse {

private int code;
private String errorMessage;

//Datastore
public CoDataList<CoDataMap<String, String>> data = new CoDataList<CoDataMap<String, String>>();
}

CoDataList 对象:
public class CoDataList <V> implements Map<Integer, V>{
int nextIndex;

public Map<Integer, V> data = new HashMap<Integer,V>();

}

CoDataMap 对象:
public class CoDataMap <K, V> implements Map<K, V>{ 
public Map<K, V> data = new HashMap<K,V>();
}

最佳答案

我只是通过在响应调用中调用以下方法来解决我的问题:

.type(MediaType.APPLICATION_JSON_TYPE)

我更改的所有内容都是 web.xml 和客户端中的 init-params,正如 Spencer-Kormos 建议的那样,并将上述行添加到调用中。

现在客户端看起来如下:
rO = service.path("check-in")
.type(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON)
.put(CoResponse.class, rO);

关于json - Jersey 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com