如何将 Map<Person, Person> 类型的 JSON 输入传递给

我有一个 PUT 类型的 JAX-RS REST 端点,我应该将一个 Map 传递给这个 API。


@PUT

@Path("/some/path")

@Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML,

        MediaType.TEXT_XML, MediaType.APPLICATION_JSON })

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

public Response updatePerson(HashMap<Person, Person> map) {


//some code here

}

我为 Person 类生成了 JSON,但我无法将它作为 JSON 输入传递给这个 API。我正在使用 Postman 客户端,当我尝试将 JSON 输入作为键值对传递时,它显示语法错误。为 Person 生成的 JSON 如下所示


  {"name":"abc","weight":100.0,"id":"123"}

我需要将此作为键值对作为映射传递。就像是


 {

   {"name":"abc","weight":100.0,"id":"123"} : 

   {"name":"def","weight":200.0,"id":"123"}

 }

任何指示我该怎么做?


慕仙森
浏览 119回答 1
1回答

一只名叫tom的猫

一般来说,这样创建看起来是个坏主意Map。JSON Object可以转换为Java Mapwhere keyisString和valueis any Object:可以是另一种Map,array或POJO简单类型。所以,通常你JSON应该看起来像:{&nbsp; &nbsp; "key" : { .. complex nested object .. }}没有其他选择。如果你想在Java映射POJO->POJO你需要指示反序列化器如何将JSON- String-转换key为一个对象。没有其他选择。我将尝试使用Jackson库来解释这个过程,因为它最常用于RESTful Web Services. 让我们定义Person适合您的JSON有效负载的类。class Person {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private double weight;&nbsp; &nbsp; private int id;&nbsp; &nbsp; public Person() {&nbsp; &nbsp; }&nbsp; &nbsp; public Person(String value) {&nbsp; &nbsp; &nbsp; &nbsp; String[] values = value.split(",");&nbsp; &nbsp; &nbsp; &nbsp; name = values[0];&nbsp; &nbsp; &nbsp; &nbsp; weight = Double.valueOf(values[1]);&nbsp; &nbsp; &nbsp; &nbsp; id = Integer.valueOf(values[2]);&nbsp; &nbsp; }&nbsp; &nbsp; public Person(String name, double weight, int id) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.weight = weight;&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public double getWeight() {&nbsp; &nbsp; &nbsp; &nbsp; return weight;&nbsp; &nbsp; }&nbsp; &nbsp; public void setWeight(double weight) {&nbsp; &nbsp; &nbsp; &nbsp; this.weight = weight;&nbsp; &nbsp; }&nbsp; &nbsp; public int getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(int id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; &nbsp; if (o == null || getClass() != o.getClass()) return false;&nbsp; &nbsp; &nbsp; &nbsp; Person person = (Person) o;&nbsp; &nbsp; &nbsp; &nbsp; return id == person.id;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; return Objects.hash(id);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return name + "," + weight + "," + id;&nbsp; &nbsp; }}因为它被用作Mapkey 我们需要实现hashCode和equals方法。除了public Person(String value)构造函数和toString方法,其他一切看起来都很正常。现在,让我们看一下这个构造函数和toString方法。它们是相关的:从实例构建,构造toString函数从. 我们可以将第一次转换称为序列化,然后将第二次转换称为序列化和反序列化中密钥的反序列化。(这两个是否很好实现这是另一回事。我只是想展示一个背后的想法。在生产上使用之前应该改进)StringPersonPersonStringMap让我们使用这些知识和Jackson特性来序列化和反序列化Map<Person, Person>:import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.KeyDeserializer;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.type.MapType;import java.util.HashMap;import java.util.Map;import java.util.Objects;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; // register deserializer for Person as keys.&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule module = new SimpleModule();&nbsp; &nbsp; &nbsp; &nbsp; module.addKeyDeserializer(Person.class, new PersonKeyDeserializer());&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(module);&nbsp; &nbsp; &nbsp; &nbsp; mapper.enable(SerializationFeature.INDENT_OUTPUT);&nbsp; &nbsp; &nbsp; &nbsp; // Create example Map&nbsp; &nbsp; &nbsp; &nbsp; Person key = new Person("Rick", 80.5, 1);&nbsp; &nbsp; &nbsp; &nbsp; Person value = new Person("Morty", 40.1, 2);&nbsp; &nbsp; &nbsp; &nbsp; Map<Person, Person> personMap = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; personMap.put(key, value);&nbsp; &nbsp; &nbsp; &nbsp; // Serialise Map to JSON&nbsp; &nbsp; &nbsp; &nbsp; String json = mapper.writeValueAsString(personMap);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(json);&nbsp; &nbsp; &nbsp; &nbsp; // Deserialise it back to `Object`&nbsp; &nbsp; &nbsp; &nbsp; MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, Person.class, Person.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.readValue(json, mapType).toString());&nbsp; &nbsp; }}class PersonKeyDeserializer extends KeyDeserializer {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object deserializeKey(String key, DeserializationContext ctxt) {&nbsp; &nbsp; &nbsp; &nbsp; return new Person(key);&nbsp; &nbsp; }}上面的代码打印为 first JSON:{&nbsp; "Rick,80.5,1" : {&nbsp; &nbsp; "name" : "Morty",&nbsp; &nbsp; "weight" : 40.1,&nbsp; &nbsp; "id" : 2&nbsp; }}如您所见,Person的toString方法用于生成JSON key. 正常序列化过程序列化为Person对象JSON。如下第二个文本被打印:{Rick,80.5,1=Morty,40.1,2}这是Map它的键和值的默认表示。因为两者都是Person对象,所以toString调用它的方法。正如您所看到的,有一个发送方式的选项,JSON但Map<Person, Person>密钥应该以某种方式表示。您需要查看Person类实现。也许您会发现与我的示例有一些相似之处。如果不是,也许它以某种方式配置。首先尝试发送PostMan:{&nbsp; &nbsp;"123" : {"name":"def","weight":200.0,"id":"123"}}或者:{&nbsp; &nbsp;"{\"name\":\"abc\",\"weight\":100.0,\"id\":\"123\"}":{&nbsp; &nbsp; &nbsp; "name":"def",&nbsp; &nbsp; &nbsp; "weight":200.0,&nbsp; &nbsp; &nbsp; "id":"123"&nbsp; &nbsp;}}也许它会起作用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java