为什么此 HashMap 在 Jersey/Tomcat 中格式不正确?
Why this HashMap not formatted correctly in Jersey/Tomcat?
我正在测试泽西岛,我想制作一个模拟端点来生成这个 JSON 对象
{
"Flight1" : 757,
"Flight2" : 107,
"Flight3" : 637,
}
所以我写了这个资源:
@GET
@Path("myjson")
@Produces(MediaType.APPLICATION_JSON)
public String getMyJson(@QueryParam ("test1") String lat, @QueryParam("test2") String lng) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Flight 1", 765);
map.put("Flight 2", 657);
map.put("Flight 3", 908);
return map.toString();
}
但是当我调用 /myjson
时我得到了这个响应
{
Flight 1=765,
Flight 2=657,
Flight 3=908
}
Jersey 已经知道哪个元素是字符串,哪个元素是整数,但它的格式就好像它们都是数字一样。
此外 Json 不能被 "pretty" 格式化程序格式化,我相信这会使 http 客户端解析变得困难。
所以我的问题是:
为什么会这样?
如何避免它并编写简单的 mock JSON 对象以进行正确格式化的测试
Why is this happening?
因为您只是在为 HashMap 创建一个 toString。
前任。
HashMap<String,String> stringStringHashMap = new HashMap<String, String>();
stringStringHashMap.put("a","b");
stringStringHashMap.put("b","b");
stringStringHashMap.put("c","b");
将打印 {b=b, c=b, a=b}
How to avoid it and write simple mock JSON object for testing that is
correctly formatted
您可以使用很多库(Gson、Jackson、JsonSimple 等)来做到这一点。
因为这已经回答了你想要做什么 HashMap to Json
这与Jersey/Tomcat无关。对于核心 Java 编程,这就是 toString()
方法处理 map
到 String
的最佳方式。
为此,您可以使用
转换为 JSONObject
String jon = JSONObject.valueToString(map);
System.out.println(jon);
或者甚至使用 gson
之类的
Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);
您可以添加Jaxb注解,直接对响应对象进行序列化和反序列化,无需转换。为此,您需要添加 jersey 的 jaxb 库,以便在启动 jersey 环境时,它可以启用自动转换功能。
示例:
@Path("jaxbResource")
@Produces("application/xml")
@Consumes("application/xml")
public class UserResource {
@GET
public User[] getUserArray() {
List<User> userList = new ArrayList<User>();
userList.add(new User(1, "John"));
………
return userList.toArray(new User[userList.size()]);
}
}
@XmlRootElement
public class User {
private int id;
private String name;
public User() {}
public User(int id,String name) {
this.id = id;
this.name = name;
}
………
}
希望对您有所帮助!!!
我正在测试泽西岛,我想制作一个模拟端点来生成这个 JSON 对象
{
"Flight1" : 757,
"Flight2" : 107,
"Flight3" : 637,
}
所以我写了这个资源:
@GET
@Path("myjson")
@Produces(MediaType.APPLICATION_JSON)
public String getMyJson(@QueryParam ("test1") String lat, @QueryParam("test2") String lng) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Flight 1", 765);
map.put("Flight 2", 657);
map.put("Flight 3", 908);
return map.toString();
}
但是当我调用 /myjson
时我得到了这个响应
{ Flight 1=765, Flight 2=657, Flight 3=908 }
Jersey 已经知道哪个元素是字符串,哪个元素是整数,但它的格式就好像它们都是数字一样。 此外 Json 不能被 "pretty" 格式化程序格式化,我相信这会使 http 客户端解析变得困难。
所以我的问题是:
为什么会这样?
如何避免它并编写简单的 mock JSON 对象以进行正确格式化的测试
Why is this happening?
因为您只是在为 HashMap 创建一个 toString。 前任。
HashMap<String,String> stringStringHashMap = new HashMap<String, String>();
stringStringHashMap.put("a","b");
stringStringHashMap.put("b","b");
stringStringHashMap.put("c","b");
将打印 {b=b, c=b, a=b}
How to avoid it and write simple mock JSON object for testing that is correctly formatted
您可以使用很多库(Gson、Jackson、JsonSimple 等)来做到这一点。 因为这已经回答了你想要做什么 HashMap to Json
这与Jersey/Tomcat无关。对于核心 Java 编程,这就是 toString()
方法处理 map
到 String
的最佳方式。
为此,您可以使用
转换为JSONObject
String jon = JSONObject.valueToString(map);
System.out.println(jon);
或者甚至使用 gson
之类的
Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);
您可以添加Jaxb注解,直接对响应对象进行序列化和反序列化,无需转换。为此,您需要添加 jersey 的 jaxb 库,以便在启动 jersey 环境时,它可以启用自动转换功能。
示例:
@Path("jaxbResource")
@Produces("application/xml")
@Consumes("application/xml")
public class UserResource {
@GET
public User[] getUserArray() {
List<User> userList = new ArrayList<User>();
userList.add(new User(1, "John"));
………
return userList.toArray(new User[userList.size()]);
}
}
@XmlRootElement
public class User {
private int id;
private String name;
public User() {}
public User(int id,String name) {
this.id = id;
this.name = name;
}
………
}
希望对您有所帮助!!!