web-dev-qa-db-de.com

JsonMappingException: Der Wert des Typs [einfacher Typ, a.b.c.Company] von JSON-String kann nicht instanziiert werden. Keine Single-String-Konstruktor-/Factory-Methode

Ich habe gerade eine REST -API zu meinem vorhandenen Spring + BlazeDS + Hibernate-Server hinzugefügt. Alles scheint zu funktionieren, wenn Daten abgerufen und als JSON serialisiert werden, aber wenn ich versuche, POST -Deserialisierungsdaten zu erstellen ein POJO bekomme ich eine Ausnahme.

Ich hatte den Eindruck, dass die Frühlingskommentare und das Vorhandensein der Jackson-Gläser im Klassenpfad alles sein würden, was zumindest für meine Liste erforderlich war, und löschte Methoden mit einfachen Parametern.

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.twoh.dto.Company] from JSON String; no single-String constructor/factory method

Hier ist die Methode, die aufgerufen wird:

public abstract class BaseEntityService<T extends BaseEntity> implements IBaseEntityService<T> {

private IBaseEntityDAO<T> DAO;

@Autowired
private ValidationResultHelper validationResultHelper;

public void setDAO(IBaseEntityDAO<T> DAO) {
    this.DAO = DAO;
}

...
@Secured("ROLE_USER")
@RequestMapping(value="/create", method=RequestMethod.POST)
public @ResponseBody ValidationResult create(@RequestBody T entity) {
    ValidationResult result = null;
    try {
        result = DAO.persistEntity(entity);
    } catch(JDBCException e) {
        result = ExceptionHelper.getValidationResult(e);
    } catch(DataIntegrityViolationException e) {
        result = ExceptionHelper.getValidationResult(e);
    }
    validationResultHelper.log(DAO.getSession(), entity.getId(), entity.getClass(), result);
    return result;
}
}

und hier ist die volle Ausnahme:

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.twoh.dto.Company] from JSON String; no single-String constructor/factory method
at org.codehaus.jackson.map.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.Java:379)
at org.codehaus.jackson.map.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.Java:268)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.Java:759)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.Java:585)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.Java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.Java:1914)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.Java:135)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.Java:154)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.Java:633)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.Java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.Java:346)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.Java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.Java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.Java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:669)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:585)
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:717)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:290)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.Java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:76)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:235)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:311)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:116)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:101)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:182)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:323)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:173)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:167)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:235)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:206)
at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:233)
at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:191)
at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:127)
at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:102)
at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:109)
at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:298)
at org.Apache.coyote.http11.Http11Processor.process(Http11Processor.Java:857)
at org.Apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.Java:588)
at org.Apache.Tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.Java:489)
at Java.lang.Thread.run(Unknown Source)

Update: Definition der Firma DTO hinzugefügt

@CheckDictionaryProperty.List({
    @CheckDictionaryProperty(propertyName="partyId", dictionaryName="Party")
})
@Unique.List({
    @Unique(properties = {"code"}, message = "UNIQUE_CODE"),
    @Unique(properties = {"name"}, message = "UNIQUE_NAME")
})
@Entity
@FXClass
@Table(name="edrcompany")
@JsonAutoDetect
public class Company extends BaseEntity {

    private static final long serialVersionUID = 1L;

    public Company(){}

    @NotBlank
    @Column
    private String name;
    public String getName(){ return this.name; }
    public void setName(String name){ this.name = name; }

    @Column
    private String code;
    public String getCode() { return this.code; }
    public void setCode(String code) { this.code = code; }

    @NotNull
    @Column(name="party_id")
    private Integer partyId;
    public Integer getPartyId() { return this.partyId; }
    public void setPartyId(Integer partyId) { this.partyId = ValueHelper.isNullOrZero(partyId) ? null : partyId; }

    @ElementCollection(targetClass=Integer.class, fetch=FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @CollectionTable(name="edrcompanyadminlink", joinColumns={@JoinColumn(name="company_id")})
    @Column(name="user_id")
    private Collection<Integer> adminUserIdList = new HashSet<Integer>();
    public Collection<Integer> getAdminUserIdList() { return this.adminUserIdList; }
    public void setAdminUserIdList (Collection<Integer> adminUserIdList) { this.adminUserIdList = adminUserIdList; }    


}


@MappedSuperclass
@FXClass
public abstract class BaseEntity implements Serializable  {

    private static final long serialVersionUID = 1L;

    public BaseEntity(){}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = ValueHelper.isNullOrZero(id) ? null : id; }

    @Column(name="ENTITY_UID", unique=true, nullable=false, updatable=false, length=36)
    /* Assign a default whenever this class is instantiated Hibernate will 
     * overwrite it when retrieving an entity from the DB.
     */
    private String uid = UUID.randomUUID().toString();
    public String getUID() { return uid; };
    public void setUID(String uid) { this.uid = uid; }

    @Version
    @Column
    private Integer version;
    @FXIgnore
    public Integer getVersion() { return this.version; }
    public void setVersion(Integer version) { this.version = version; }

    // Fake property so that DTO2FX will put it in
    public String getClassName() { return this.getClass().getName(); }
    @JsonIgnore
    public void setClassName(String className) { throw new UnsupportedOperationException(); }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || !(o instanceof BaseEntity)) return false;

        BaseEntity other = (BaseEntity) o;

        // if the id is missing, return false
        if (uid == null) return false;

        // equivalence by uid
        return uid.equals(other.getUID());
    }

    @Override
    public int hashCode() {
        if (uid != null) {
            return uid.hashCode();
        } else {
            return super.hashCode();
        }
    }

    @Override
    public String toString() {
        return this.getClassName() + ": " + this.getId();
    }

}

Update Wenn ich das DTO ändere, sodass Jackson die Company.adminUserIdList -Eigenschaft ignoriert, wird der Datensatz erfolgreich erstellt.

@JsonIgnore
public Collection<Integer> getAdminUserIdList() { return this.adminUserIdList; }
@JsonIgnore
public void setAdminUserIdList (Collection<Integer> adminUserIdList) { this.adminUserIdList = adminUserIdList; }    

Update Hier ist der Json, wie er von der /company/get/1-Methode mit FireFox RESTClient zurückgegeben wird

{
  "partyId":1,
  "adminUserIdList":[21],
  "name":"2H Mechanical LLC",
  "code":null,
  "uid":"fc5e15e7-a9a7-11e1-be90-7d08b05cbb96",
  "id":1,
  "className":"com.twoh.dto.Company",
  "version":0
}

Ich verwendete ein ähnliches Muster (weniger die "ID" und eine andere "Uid") für den Aufruf /compamy/create mit einem Content-type=application/json-Header

12
hairyone

Ich löste das gleiche Problem, indem ich den JSON korrigierte, den ich an den Server sendete. es war ungültig . Ich entfernte die "," - Zeichen am Ende der letzten Attribute und es funktionierte . Ich hoffe es hilft

12
Techky

In meinem Fall fehlten mir die geschweiften Klammern am Anfang und Ende.

0
Nirmal Mangal

Ähnlich wie bei @Nirmal habe ich JSON übergeben, das in doppelten Anführungszeichen enthalten war:

"{ "SomeJSON":"Value" }"

anstatt 

{ "SomeJSON":"Value" }

Durch das Entfernen des "s wurde das Problem behoben.

0
DivDiff