Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在@OneToMany集合上使用@JsonSerialize(using=MySerializer.class)<;对象>;_Java_Hibernate_Jackson_Spring Data Rest - Fatal编程技术网

Java 在@OneToMany集合上使用@JsonSerialize(using=MySerializer.class)<;对象>;

Java 在@OneToMany集合上使用@JsonSerialize(using=MySerializer.class)<;对象>;,java,hibernate,jackson,spring-data-rest,Java,Hibernate,Jackson,Spring Data Rest,在SpringBoot/SpringDataREST项目中,我遇到了在@OneToMany属性上使用自定义JsonSerializer的问题。执行HTTP GET/collection请求时,出现以下错误: 无法写入HTTP消息: org.springframework.http.converter.HttpMessageNotWritableException: 无法写入内容:无法重写序列化程序(通过 参考链: org.springframework.hateoas.Resources[“_e

在SpringBoot/SpringDataREST项目中,我遇到了在@OneToMany属性上使用自定义
JsonSerializer
的问题。执行HTTP GET/collection请求时,出现以下错误:

无法写入HTTP消息: org.springframework.http.converter.HttpMessageNotWritableException: 无法写入内容:无法重写序列化程序(通过 参考链: org.springframework.hateoas.Resources[“_embedded”]->java.util.UnmodifiableMap[“analogParameters”]->java.util.ArrayList[0]); 嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法重写 序列化程序(通过引用链: org.springframework.hateoas.Resources[“_embedded”]->java.util.UnmodifiableMap[“analogParameters”]->java.util.ArrayList[0])

下面是我的实体类的摘录:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="output_parameter_id")
@JsonSerialize(using=InputParametersSerializer.class)
//@Transcient
private Set<InputParameter> inputParameters = new HashSet<InputParameter>();

public Set<InputParameter> getInputParameters() {
    return inputParameters;
}

public void setInputParameters(Set<InputParameter> inputParameters) {
    this.inputParameters = inputParameters;
}
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name=“output\u parameter\u id”)
@JsonSerialize(使用=InputParametersSerializer.class)
//@易怒的
私有集inputParameters=new HashSet();
公共设置getInputParameters(){
返回输入参数;
}
public void setInputParameters(设置inputParameters){
this.inputParameters=inputParameters;
}
JsonSerializer

公共类输入参数序列化程序
扩展JsonSerializer{
静态最终长SerialVersionId=123L;
public void序列化(设置ips、JsonGenerator jg、,
序列化提供程序(sp)
抛出IOException{
jg.writeString(“耶”);
}
}
如果我删除@OneToMany并将属性定义为@transient,它将按预期工作

InputParameter实体没有关联的存储库(它不会作为rest资源导出)


如何在@OneToMany属性上使用JsonSerializer?

在使用Spring Boot 2.1.0时,我遇到了一个非常类似的问题。添加带有
using
keycusing
的自定义序列化程序效果很好,但是带有
@OneToMany
注释字段的自定义反序列化程序会抛出相同的
JsonMappingException:无法覆盖您得到的序列化程序
消息,而带有
@ElementCollection
的自定义反序列化程序只会被忽略。我怀疑SpringDataREST为了处理这些字段的(反)序列化而做了一些没有文档记录的魔术,但是添加自定义反序列化器并不能很好地处理这些字段。我的解决方法是通过带注释的getter和setter添加一个额外的JSON字段。以您的示例为例,它将如下所示:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="output_parameter_id")
private Set<InputParameter> inputParameters = new HashSet<InputParameter>();

public Set<InputParameter> getInputParameters() {
    return inputParameters;
}

public void setInputParameters(Set<InputParameter> inputParameters) {
    this.inputParameters = inputParameters;
}


@JsonSerialize(using=InputParametersSerializer.class)
public Set<InputParameter> getInputParametersSet() {
    return getInputParameters();
}


@JsonDeserialize(using=InputParametersDeserializer.class)
public void setInputParametersSet(Set<InputParameter> inputParameters) {
    setInputParameters(inputParameters);
}
虽然不理想,但该字段的序列化和反序列化工作正常。 或者,为了保留字段名,类似的解决方法适用于
@ElementCollection
,但不适用于
@OneToMany

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="output_parameter_id")
@JsonIgnore
private Set<InputParameter> inputParameters = new HashSet<InputParameter>();

public Set<InputParameter> getInputParameters() {
    return inputParameters;
}

public void setInputParameters(Set<InputParameter> inputParameters) {
    this.inputParameters = inputParameters;
}


@JsonProperty("inputParameters")
@JsonSerialize(using=InputParametersSerializer.class)
public Set<InputParameter> getInputParametersSet() {
    return getInputParameters();
}

@JsonProperty("inputParameters")
@JsonDeserialize(using=InputParametersDeserializer.class)
public void setInputParametersSet(Set<InputParameter> inputParameters) {
    setInputParameters(inputParameters);
}
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name=“output\u parameter\u id”)
@杰索尼奥雷
私有集inputParameters=new HashSet();
公共设置getInputParameters(){
返回输入参数;
}
public void setInputParameters(设置inputParameters){
this.inputParameters=inputParameters;
}
@JsonProperty(“inputParameters”)
@JsonSerialize(使用=InputParametersSerializer.class)
公共集getInputParametersSet(){
返回getInputParameters();
}
@JsonProperty(“inputParameters”)
@JsonDeserialize(使用=InputParametersDeserializer.class)
public void setInputParametersSet(设置inputParameters){
设置输入参数(输入参数);
}

最后,我不得不采用第一种方法。

您的
InputParameter
是否引用了您发布的实体摘录??不,它是另一个名为“Parameter”的实体(名称相似,但它们是不同的实体),因此最好将关注点分开,即使用DTO(通过RestController公开,带有json相关注释)与实体分离(使用JPA或Hibernate映射到数据库的对象)。看见
{
...
  "inputParameters" : ...,
  "inputParametersSet" : ...,
...
}
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="output_parameter_id")
@JsonIgnore
private Set<InputParameter> inputParameters = new HashSet<InputParameter>();

public Set<InputParameter> getInputParameters() {
    return inputParameters;
}

public void setInputParameters(Set<InputParameter> inputParameters) {
    this.inputParameters = inputParameters;
}


@JsonProperty("inputParameters")
@JsonSerialize(using=InputParametersSerializer.class)
public Set<InputParameter> getInputParametersSet() {
    return getInputParameters();
}

@JsonProperty("inputParameters")
@JsonDeserialize(using=InputParametersDeserializer.class)
public void setInputParametersSet(Set<InputParameter> inputParameters) {
    setInputParameters(inputParameters);
}