无法反序列化实例Jackson和JSON

时间:2016-02-10 23:43:44

标签: java json spring serialization

每次我尝试保存凭证实体对象时,我都会收到此错误。

  

[WARN] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法反序列化com.portal的实例。 app.domain.Credentials超出START_ARRAY令牌    在[来源:java.io.PushbackInputStream@246feef4; line:1,column:1];嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化com.wiportal.app.domain.Credentials的实例    在[来源:java.io.PushbackInputStream@246feef4; line:1,column:1]

这是我的凭据域类的代码:

package com.portal.app.domain;


import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.validation.constraints.NotNull;
import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
* A Credentials.
*/
@Entity
@Table(name = "credentials")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "credentials")
public class Credentials implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
@Column(name = "password", nullable = false)
private String password;

@NotNull
@Column(name = "secret_question1", nullable = false)
private String secretQuestion1;

@NotNull
@Column(name = "secret_question2", nullable = false)
private String secretQuestion2;

@NotNull
@Column(name = "secret_answer1", nullable = false)
private String secretAnswer1;

@NotNull
@Column(name = "secret_answer2", nullable = false)
private String secretAnswer2;

@JsonIgnore
@OneToOne(mappedBy = "credentials", cascade=CascadeType.ALL)
private User user;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getSecretQuestion1() {
    return secretQuestion1;
}

public void setSecretQuestion1(String secretQuestion1) {
    this.secretQuestion1 = secretQuestion1;
}

public String getSecretQuestion2() {
    return secretQuestion2;
}

public void setSecretQuestion2(String secretQuestion2) {
    this.secretQuestion2 = secretQuestion2;
}

public String getSecretAnswer1() {
    return secretAnswer1;
}

public void setSecretAnswer1(String secretAnswer1) {
    this.secretAnswer1 = secretAnswer1;
}

public String getSecretAnswer2() {
    return secretAnswer2;
}

public void setSecretAnswer2(String secretAnswer2) {
    this.secretAnswer2 = secretAnswer2;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    Credentials credentials = (Credentials) o;
    return Objects.equals(id, credentials.id);
}

@Override
public int hashCode() {
    return Objects.hashCode(id);
}

@Override
public String toString() {
    return "Credentials{" +
        "id=" + id +
        ", password='" + password + "'" +
        ", secretQuestion1='" + secretQuestion1 + "'" +
        ", secretQuestion2='" + secretQuestion2 + "'" +
        ", secretAnswer1='" + secretAnswer1 + "'" +
        ", secretAnswer2='" + secretAnswer2 + "'" +
        '}';
}

}

这是我的休息资源的代码:

package com.portal.app.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.portal.app.domain.Credentials;
import com.portal.app.repository.CredentialsRepository;
import com.portal.app.repository.search.CredentialsSearchRepository;
import com.portal.app.web.rest.util.HeaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
* REST controller for managing Credentials.
*/
@RestController
@RequestMapping("/api")
public class CredentialsResource {

private final Logger log =         LoggerFactory.getLogger(CredentialsResource.class);

@Inject
private CredentialsRepository credentialsRepository;

@Inject
private CredentialsSearchRepository credentialsSearchRepository;

/**
 * POST  /credentialss -> Create a new credentials.
 */
@RequestMapping(value = "/credentialss",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Credentials> createCredentials(@Valid @RequestBody Credentials credentials) throws URISyntaxException {
    log.debug("REST request to save Credentials : {}", credentials);
    if (credentials.getId() != null) {
        return ResponseEntity.badRequest().header("Failure", "A new credentials cannot already have an ID").body(null);
    }
    Credentials result = credentialsRepository.save(credentials);
    credentialsSearchRepository.save(result);
    return ResponseEntity.created(new URI("/api/credentialss/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert("credentials", result.getId().toString()))
        .body(result);
}

/**
 * PUT  /credentialss -> Updates an existing credentials.
 */
@RequestMapping(value = "/credentialss",
    method = RequestMethod.PUT,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Credentials> updateCredentials(@Valid @RequestBody Credentials credentials) throws URISyntaxException {
    log.debug("REST request to update Credentials : {}", credentials);
    if (credentials.getId() == null) {
        return createCredentials(credentials);
    }
    Credentials result = credentialsRepository.save(credentials);
    credentialsSearchRepository.save(credentials);
    return ResponseEntity.ok()
        .headers(HeaderUtil.createEntityUpdateAlert("credentials", credentials.getId().toString()))
        .body(result);
}

/**
 * GET  /credentialss -> get all the credentialss.
 */
@RequestMapping(value = "/credentialss",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Credentials> getAllCredentialss(@RequestParam(required = false) String filter) {
    if ("user-is-null".equals(filter)) {
        log.debug("REST request to get all Credentialss where user is null");
        return StreamSupport
            .stream(credentialsRepository.findAll().spliterator(), false)
            .filter(credentials -> credentials.getUser() == null)
            .collect(Collectors.toList());
    }

    log.debug("REST request to get all Credentialss");
    return credentialsRepository.findAll();
}

/**
 * GET  /credentialss/:id -> get the "id" credentials.
 */
@RequestMapping(value = "/credentialss/{id}",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Credentials> getCredentials(@PathVariable Long id) {
    log.debug("REST request to get Credentials : {}", id);
    return Optional.ofNullable(credentialsRepository.findOne(id))
        .map(credentials -> new ResponseEntity<>(
            credentials,
            HttpStatus.OK))
        .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

/**
 * DELETE  /credentialss/:id -> delete the "id" credentials.
 */
@RequestMapping(value = "/credentialss/{id}",
    method = RequestMethod.DELETE,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Void> deleteCredentials(@PathVariable Long id) {
    log.debug("REST request to delete Credentials : {}", id);
    credentialsRepository.delete(id);
    credentialsSearchRepository.delete(id);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("credentials", id.toString())).build();
}

/**
 * SEARCH  /_search/credentialss/:query -> search for the credentials corresponding
 * to the query.
 */
@RequestMapping(value = "/_search/credentialss/{query}",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Credentials> searchCredentialss(@PathVariable String query) {
    return StreamSupport
        .stream(credentialsSearchRepository.search(queryStringQuery(query)).spliterator(), false)
        .collect(Collectors.toList());
}

}

我在前端创建一个与脚本的所有属性匹配的java脚本对象,但每次尝试保存时都会出现错误。

0 个答案:

没有答案