找不到合适的创建者方法来从 JSON 字符串反序列化

发布于 2024-12-11 21:40:37 字数 7107 浏览 0 评论 0原文

我在 Spring 应用程序中使用 Jackson。当 Jackson 将我的 JSON 数据转换为对象时,出现错误。

我的 JSON 数据是:

{"name":"sdfg","username":"dfgdg","password":"dfgdg","type":"A","protocol":"1", "description":"sdfsdfdsf"}

我的课程是:

public class Device extends Name {

    @Column
    private String username;

    @Column
    private String password;

    @JsonUnwrapped// I added this tag, not sure to use
    @ManyToOne
    private DeviceType type; 

    @JsonUnwrapped// I added this tag, not sure to use
    @ManyToOne
    private Protocol protocol;
    ...
    //description and name are fields at Name that Device extends
    //getters and setters

/* I tried that not sure:
 @JsonProperty("protocol")
    public void setProtocol(Protocol protocol) {
        this.protocol = protocol;
    }
*/
}

public class DeviceType extends Name {

    private List<Protocol> supportedProtocols;

    public List<Protocol> getSupportedProtocols() {
        return supportedProtocols;
    }

/*
I tried to add that:
@JsonProperty("type")
    @Override
    public void setName(String name) {
        super.setName(name);
    }
*/
}

public class Protocol extends Name {     
 /*
  Nothing at this method. I added that for my purpose:
  JsonProperty("protocol")
  @Override
  public void setName(String name) {
    super.setName(name);
  }
 */
}

错误如下:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.a.a.a.DeviceType, problem: no suitable creator method found to deserialize from JSON String
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2fdaa156; line: 1, column: 47] (through reference chain: com.a.a.a.Device["type"])
    org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
    org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:210)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:530)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:356)
    org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:230)
    org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:334)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:495)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
    org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
    org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1455)
    org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
    org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

I use Jackson with my Spring application. When Jackson converts my JSON data to an object it gives me an error.

My JSON data is that:

{"name":"sdfg","username":"dfgdg","password":"dfgdg","type":"A","protocol":"1","description":"sdfsdfdsf"}

My class is that:

public class Device extends Name {

    @Column
    private String username;

    @Column
    private String password;

    @JsonUnwrapped// I added this tag, not sure to use
    @ManyToOne
    private DeviceType type; 

    @JsonUnwrapped// I added this tag, not sure to use
    @ManyToOne
    private Protocol protocol;
    ...
    //description and name are fields at Name that Device extends
    //getters and setters

/* I tried that not sure:
 @JsonProperty("protocol")
    public void setProtocol(Protocol protocol) {
        this.protocol = protocol;
    }
*/
}

public class DeviceType extends Name {

    private List<Protocol> supportedProtocols;

    public List<Protocol> getSupportedProtocols() {
        return supportedProtocols;
    }

/*
I tried to add that:
@JsonProperty("type")
    @Override
    public void setName(String name) {
        super.setName(name);
    }
*/
}

public class Protocol extends Name {     
 /*
  Nothing at this method. I added that for my purpose:
  JsonProperty("protocol")
  @Override
  public void setName(String name) {
    super.setName(name);
  }
 */
}

Error as follows:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.a.a.a.DeviceType, problem: no suitable creator method found to deserialize from JSON String
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2fdaa156; line: 1, column: 47] (through reference chain: com.a.a.a.Device["type"])
    org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
    org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:210)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:530)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:356)
    org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:230)
    org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:334)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:495)
    org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
    org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
    org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1455)
    org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
    org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-12-18 21:40:37

异常准确地解释了问题所在:它不知道如何将字符串“A”转换为DeviceType类型的值。
最简单的方法是添加构造函数,例如:

public DeviceType(String type) { .... }

并且事情会起作用。

Exception explains exactly what the problem is: it does not know how to convert String "A" into value of type DeviceType.
The easiest way would be to add constructor like:

public DeviceType(String type) { .... }

and things would work.

伪心 2024-12-18 21:40:37

试试这个它有效

{
        "name": "sdfg",
        "username": "dfgdg",
        "password": "dfgdg",
        "type": {"type_id":"1"},
        "protocol": {"protocol_id":"1"}
}

Try this it works

{
        "name": "sdfg",
        "username": "dfgdg",
        "password": "dfgdg",
        "type": {"type_id":"1"},
        "protocol": {"protocol_id":"1"}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文