gpt4 book ai didi

java - 如何创建返回 xml 和 json 的 webservice 函数?

转载 作者:行者123 更新时间:2023-11-30 09:34:31 27 4
gpt4 key购买 nike

我已经创建了一个网络服务函数:

@GET
@Path("getusers/")
@Produces({"application/xml","application/json"})
public Object getUsers()
{
String distanceQuery="SELECT UGL.user_id,UP.fname,UGL.mlatitude,UGL.mlogitude,UGL.mlogitude from user_gps_location UGL,user_profile UP where UGL.user_id=UP.user_id";
Query queryResult=em.createNativeQuery(distanceQuery);

userList=queryResult.getResultList();
return userList;
}

该函数返回结果列表:如果我在选择 application/json 选项时用我的浏览器测试它,它工作正常:

结果:

[[1,"Ankit",37.334542,-121.890821,-121.890821],
[1,"Ankit",37.337749,-121.886702,-121.886702],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",32.727798,-117.15683,-117.15683],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[2,"Niharika",37.334541666666674,-121.88670166666667,-121.88670166666667],
[2,"Niharika",37.334541666666674,-121.88670166666667,-121.88670166666667]]

但是当我选择“application/xml”时它给了我一个错误:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.Vector, and Java type class java.util.Vector, and MIME media type application/xml was not found

任何人都可以帮助我并告诉我它有什么问题吗?

另外,我如何才能返回格式正确的结果,如下所示?

<id>1</id>
<name>Ankit</name>

我知道我需要为此使用一个类,但由于这是一个 native 查询,而且我使用两个表并返回一个结果列表,我不知道该怎么做。

最佳答案

尝试改变

public Object getUsers() 

public List<MyObjectType> getUsers()

匹配类型 userList。

此外,确保 @XMLRootElement 注释存在于域类中

关于java - 如何创建返回 xml 和 json 的 webservice 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11769031/

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