没有从字符串值('1074')反序列化的字符串参数构造函数/工厂方法

Posted

技术标签:

【中文标题】没有从字符串值(\'1074\')反序列化的字符串参数构造函数/工厂方法【英文标题】:no String-argument constructor/factory method to deserialize from String value ('1074')没有从字符串值('1074')反序列化的字符串参数构造函数/工厂方法 【发布时间】:2018-09-09 04:54:28 【问题描述】:

我是 Spring DataJPA + REST 项目的新手,我正在尝试从具有 OneToOne 单向关系的引用 EmployerType 执行添加新或编辑 Employer。任务很简单,但我被卡住了。当我尝试添加或编辑数据时出现此错误:

org.springframework.http.converter.HttpMessageNotReadableException:
Could not read document: Can not construct instance of
tr.mis.domain.EmployerType: no String-argument constructor/factory
method to deserialize from String value ('1074')  at [Source:
java.io.PushbackInputStream@6d856887; line: 1, column: 609] (through
reference chain: tr.mis.domain.Employer["employerType"]); nested
exception is com.fasterxml.jackson.databind.JsonMappingException: Can
not construct instance of tr.mis.domain.EmployerType: no
String-argument constructor/factory method to deserialize from String
value ('1074')

以下是有关课程的信息。

@Entity
@Table(name = "employer")
public class Employer implements Serializable  
    @GenericGenerator(
            name = "wikiSequenceGenerator",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = 
                    @Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
                    @Parameter(name = "initial_value", value = "1000"),
                    @Parameter(name = "increment_size", value = "1")
            
    )
    @Id
    @GeneratedValue(generator = "wikiSequenceGenerator")
    private Long employerId;
    private String uid;
    private String name;
    private String address;
    private String headName;
    
    //@JsonIgnore
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   // @PrimaryKeyJoinColumn
    @JoinColumn(name = "employer_type_id")
    private EmployerType employerType;

    //Getters and Setters

EmployerType 类

 @Entity
    @Table(name = "employertype")
    
    public class EmployerType 
        @GenericGenerator(
                name = "wikiSequenceGenerator",
                strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
                parameters = 
                        @Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
                        @Parameter(name = "initial_value", value = "1000"),
                        @Parameter(name = "increment_size", value = "1")
                
        )
        @Id
        @GeneratedValue(generator = "wikiSequenceGenerator")

        private Long employerTypeId; 
        private String uid; 
        private String employerTypeName;
    
    //Getters and Setters

休息控制器

@RequestMapping(value = "/employers", method = RequestMethod.PUT)
    public Employer updateEmployer(@RequestBody Employer employer) 

        return employerRepository.save(employer);
        
    

【问题讨论】:

查看我的相关问题:1、2,也许他们可以帮助... 只需删除@JoinColumn(name = "employer_type_id")。它应该工作 【参考方案1】:

我已通过在 EmployerType 类中添加 String 构造函数来解决此问题。

 public EmployerType(String employerTypeName) 
        // TODO Auto-generated constructor stub
    

【讨论】:

以上是关于没有从字符串值('1074')反序列化的字符串参数构造函数/工厂方法的主要内容,如果未能解决你的问题,请参考以下文章

使用 Jackson 从 XML 到 POJO 的反序列化问题:没有从字符串值反序列化的字符串参数构造函数/工厂方法

Jackson MismatchedInputException(没有从字符串值反序列化的字符串参数构造函数/工厂方法)

Spring Boot:没有从字符串值反序列化的字符串参数构造函数/工厂方法

objectmapper.readValue() 失败并出现错误“没有从字符串值反序列化的字符串参数构造函数/工厂方法”

com.fasterxml.jackson.databind.JsonMappingException 没有从字符串值('1')反序列化的字符串参数构造函数/工厂方法

没有参数名称的JSON4S反序列化