2017-09-24 98 views
1

我需要將ajax請求作爲json傳遞給我的休息服務。 我的模型類是無法讀取文檔:無法反序列化java.lang.String的實例超出START_ARRAY令牌

@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(); 
     } 
    }); 

我從Ajax調用傳遞JSON數據就像

{"title":"dhfg","tags":["Interactive","Online"],"documents":[],"links":[]} 

我如何從json傳遞標籤數組?我是新Java中

錯誤的詳細信息

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: [email protected]; 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: [email protected]; line: 1, column: 24] (through reference chain: org.fs.spring.tm.model.MessageList["tags"]) 
+0

你輸入了錯誤的'List'。你想要'java.util.List'。 – chrylis

+0

@chrylis對不起,這是一個複製粘貼錯誤... –

回答

0

,我改變的模型類標籤的數據類型從

public String tags; 

public ArrayList<String> tags; 

解決我的問題:)

相關問題