Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
无法读取JSON_Json_Spring_Spring Mvc_Jackson - Fatal编程技术网

无法读取JSON

无法读取JSON,json,spring,spring-mvc,jackson,Json,Spring,Spring Mvc,Jackson,我的控制器如下所示关系类型是表类中的一个字段。我得到了以下例外。有什么想法吗 org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of com.mycompany.myapp.core.domain.RelationType from String value 'unionetomany': value not one

我的控制器如下所示<代码>关系类型是
类中的一个字段。我得到了以下例外。有什么想法吗

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of com.mycompany.myapp.core.domain.RelationType from String value 'unionetomany': value not one of declared Enum instance names: [unionetoone, unionetomany]
 at [Source: org.apache.catalina.connector.CoyoteInputStream@7b3e44cb; line: 1, column: 435] (through reference chain: com.mycompany.myapp.core.domain.Table["relations"]->com.mycompany.myapp.core.domain.Relation["relationtype"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of com.mycompany.myapp.core.domain.RelationType from String value 'unionetomany': value not one of declared Enum instance names: [unionetoone, unionetomany, unimanytoone, unimanytomany, bionetoone, bionetomany, bimanytoone, bimanytomany]
 at [Source: org.apache.catalina.connector.CoyoteInputStream@7b3e44cb; line: 1, column: 435] (through reference chain: com.mycompany.myapp.core.domain.Table["relations"]->com.mycompany.myapp.core.domain.Relation["relationtype"])
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.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)

@RequestMapping(method = RequestMethod.POST)
    public void validateSchema(@RequestBody Table[] tables, HttpServletRequest request) {
    ......
}

public enum RelationType {
  UNI_ONE_TO_ONE("unionetoone"),
  UNI_ONE_TO_MANY("unionetomany");

  private final String text;

  private RelationType(final String text) {
      this.text = text;
  }

  @Override
  public String toString() {
      return text;
  }
}

默认情况下,
Enum
s
name
用于序列化,而不是
toString()
返回的任何内容。因此,虽然错误消息令人困惑(它应该列出预期的实际值),但问题是它预期的是
UNI\u ONE\u TO \u MANY

如果您想使用由
toString()
返回的值,您应该能够将注释
@JsonValue
添加到
toString()
方法,这应该表明该值应该用于序列化

另一种方法是启用
反序列化功能。使用\u to \u STRING
读取\u ENUMS\u,这将全局更改行为