Java Jackson JSON - 解组时“没有单字符串构造函数/工厂方法”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25192342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:50:09  来源:igfitidea点击:

Hymanson JSON - "no single-String constructor/factory method" error on unmarshal

javajsonspring-mvcHymanson

提问by sat

The simplest of cases is causing me problems. I've run into it the first time. I am able to unmarshal slightly more complex json but this simple one fails.

最简单的情况给我带来了问题。我第一次遇到它。我能够解组稍微复杂的 json,但这个简单的失败了。

What would cause this and why is Hymanson having trouble with just a single string?

什么会导致这种情况,为什么Hyman逊只用一根弦就遇到问题?

A simple class holding the name of the user's role.

一个包含用户角色名称的简单类。

public class UpdateUserRole {
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }    
}

Inside the controller

控制器内部

public void updateUserRole(@PathVariable Long id, @RequestBody UpdateUserRoleReq req) {
}

As soon as Hymanson sees this, it throws this error

Hyman逊一看到这个,就会抛出这个错误

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not instantiate value of type [simple type, class com.mycompany.UpdateUserRoleReq] from String value ('{"name":"admin_1407445357682"}'); no single-String constructor/factory method; nested exception is com.fasterxml.Hymanson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.mycompany.UpdateUserRoleReq] from String value ('{"name":"admin_1407445357682"}'); no single-String constructor/factory method
    at org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter.readJavaType(MappingHymanson2HttpMessageConverter.java:228)
    at org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter.read(MappingHymanson2HttpMessageConverter.java:220)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:183)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:98)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:874)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:649)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextFilterConfiguration.doFilterInternal(EndpointWebMvcAutoConfiguration.java:257)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

回答by Samuel Waggoner

I was able to solve a very similar problem by adhering exactly to the error message.

通过严格遵守错误消息,我能够解决一个非常相似的问题。

I added the equivalent in your case of a single-String constructor.

我在您的单字符串构造函数的情况下添加了等效项。

public class UpdateUserRole {

  private String name;

  public UpdateUserRole() {}

  public UpdateUserRole(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }    
}

回答by Alvin Yue

I my case. I was sending in double escaped JSON string e.g. "{\"name\":\"abc\", \"age\":21}" and my String constructor only receive the JSON string completely. The other attributes (age) was not updated at all. Once I changed the input to {"name":"abc", "age":21}. Hymanson was able to automatically parse the JSON input into object(s).

我的情况。我正在发送双转义 JSON 字符串,例如 "{\"name\":\"abc\", \"age\":21}" 并且我的 String 构造函数只完全接收 JSON 字符串。其他属性(年龄)根本没有更新。一旦我将输入更改为 {"name":"abc", "age":21}。Hymanson 能够自动将 JSON 输入解析为对象。

回答by Mike

You have to add the construct method which contains single parameter. Just have a try.

您必须添加包含单个参数的构造方法。试试吧。

回答by dobrivoje

I folowed a Jira tutorial (https://developer.atlassian.com/server/jira/platform/oauth/) for OAuth. In my case, I used dto to json transformation with GSON lib, however I couldn't make it work. I'll provide source later, but for fast resolve, please see : https://stackoverflow.com/a/59981597/1551368

我遵循了OAuth的 Jira 教程(https://developer.atlassian.com/server/jira/platform/oauth/)。就我而言,我使用 dto 到 GSON lib 进行 json 转换,但是我无法使其工作。我稍后会提供源代码,但为了快速解决,请参阅:https: //stackoverflow.com/a/59981597/1551368