当前位置: 首页 > 知识库问答 >
问题:

无法读取文档:无法从START_ARRAY标记反序列化java.lang.String实例

田宇
2023-03-14
@Entity
@Table(name = "messageslist")
public class MessageList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String title;    
public String tags;
public String documents;
public String links;
//Then getters and setters
}
@RequestMapping(value = "/AddPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<MessageList> addPost(@RequestBody @Valid MessageList messageList, BindingResult bindingResult,
        UriComponentsBuilder ucBuilder) {
    BindingErrorsResponse errors = new BindingErrorsResponse();
    HttpHeaders headers = new HttpHeaders();
    if (bindingResult.hasErrors() || (messageList == null)) {
        errors.addAllErrors(bindingResult);
        headers.add("errors", errors.toJSON());
        return new ResponseEntity<MessageList>(headers, HttpStatus.BAD_REQUEST);
    }

    this.taskManagerService.savePost(messageList);
    headers.setLocation(ucBuilder.path("/api/TS/{id}").buildAndExpand(messageList.getId()).toUri());
    return new ResponseEntity<MessageList>(messageList, headers, HttpStatus.CREATED);
}
var post = JSON.stringify({
        title : title,
        tags : tags,
        documents : documents,
        links : links
    });

    var url = "http://localhost:8080/TS/AddPost";
    $.ajax({
        type : 'POST',
        url : url,
        contentType : 'application/json; charset=utf-8',
        data : post,
        dataType : 'json',
        success : function(data) {
            onComplete();
        }
    });
{"title":"dhfg","tags":["Interactive","Online"],"documents":[],"links":[]}
2017-09-24 01:40:31.978  WARN 9600 --- [nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@459b29e9; line: 1, column: 24] (through reference chain: org.fs.spring.tm.model.MessageList["tags"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: java.io.PushbackInputStream@459b29e9; line: 1, column: 24] (through reference chain: org.fs.spring.tm.model.MessageList["tags"])

共有1个答案

章侯林
2023-03-14

我将模型类中标记的数据类型从

public String tags;

public ArrayList<String> tags;

解决了我的问题:)

 类似资料: