我有一个像这样的web服务方法。我将通过POST发送JSON映射到该方法,但它没有正确设置address字段。我知道JSON是有效的,因为它直接从一个将一个人转储到JSON并读回它的单元测试出来。测试通过(反序列化的对象有2个电话号码),这是确切的字符串。
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Person entity) {
System.out.println("Saving entity: " + entity);
super.create(entity);
System.out.println("Saved: " + entity);
}
当我发布JSON时,它不会在对象上设置phone numbers映射。以下是输出:
信息:保存实体:Person{id=null,firstname=john,nickname=jj,lastname=smith,middlenames=[Stelling,Deering],idnum=js3234,ismale=n,birthday=null,phoneNumbers=null}
信息:已保存:Person{id=2,FirstName=John,NickName=JJ,LastName=Smith,Middlenames=[Stelling,Deering],idnum=JS3234,ismale=N,birthday=null,PhoneNumbers=null}
下面是JSON:
{
"id": null,
"firstName": "John",
"nickname": "JJ",
"lastName": "Smith",
"middleNames": [
"Stelling",
"Deering"
],
"idNum": "js3234",
"isMale": "n",
"birthday": 778266673889,
"phoneNumbers": {
"Personal Mobile": {
"countryCode": "26",
"areaCode": "200",
"subscriberNubmer": "4069942",
"extension": null
},
"Home": {
"countryCode": "79",
"areaCode": "115",
"subscriberNubmer": "9518863",
"extension": null
}
}
}
这些物体如下所示:
@Entity
@Table(name = "ent_person")
public class Person implements Serializable, Comparable<Person> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private static final long serialVersionUID = -4680156785318108346L;
protected String firstName;
protected String nickname;
protected String lastName;
@ElementCollection(fetch = FetchType.EAGER)
protected List<String> middleNames;
protected String idNum;
protected char isMale;
@Temporal(value = TemporalType.DATE)
protected Date birthday;
@ElementCollection(fetch=FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
protected Map<String, PhoneNumber> phoneNumbers;
--- Snipped getters, setters, to string and constructors --
这是PhoneNumber对象
public class PhoneNumber implements Serializable {
private static final long serialVersionUID = -423634682785318106L;
public static transient final String HOME = "Home";
public static final String PERSONAL_MOBILE = "Personal Mobile";
public static final String OFFICE = "Office";
public static final String WORK_MOBILE = "Work Mobile";
public static final String FAX = "Fax";
public static final String PAGER = "Pager";
public static final String TOLL_FREE = "Toll Free";
public static final String OTHER = "Other";
String countryCode;
String areaCode;
String subscriberNubmer;
String extension;
好了,我终于找到答案了。映射不会根据此表自动反序列化
我必须添加一个自定义的MessageBodyReader来读取字符串。因为我已经有了一个可以工作的单元测试来读取字符串,所以添加正确的类和注释就很简单了,然后一切都正常了:
@Consumes("application/json")
@Provider
public class PersonReader
implements MessageBodyReader<Person> {
ObjectMapper mapper;
public PersonReader(){
mapper = new ObjectMapper();
}
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == Person.class;
}
public Person readFrom(Class<Person> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
Person p = mapper.readValue(entityStream, Person.class);
return p;
}
}
传入我的应用程序ID。我在某处读到配置在解析服务器上不起作用,但想确认一下
只有。这些服务到底有什么不同?端口到底做什么?
我可以想象这样一个解决方案:一个子映射器,在这个映射器中,我用如下所示的查找重写Dto to Domain方法: 但目标在MapStruct中是必需的。也许我可以以某种方式指定整个对象作为目标?
我有tje下面的映射器,我使用服务通过代码获取实体
我在youtube上按部就班地学习教程,但我没有得到结果。代码如下: 主要应用 或来自Chrome的以下内容: 白标签错误页 此应用程序没有/error的显式映射,因此您将其视为一种后退。
我使用flink动态分析json类型的数据,对keyby和给定的列求和,在我的mapFunction中,我将json转换为case类,但结果流没有在keyby函数中得到编译器,在线程“main”org.apache.flink.api.common.InvalidProgramException中得到错误。我的代码如下所示 我如何将json转换为case类或tuple?