从输入流 Dropwizard 示例中读取实体时出错

Error reading entity from input stream Dropwizard example

我正在尝试像下面那样使用这个 dropwizard example and build off of it. I tried to add a column userName to the people table in Person.java

public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

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

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

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

public Person() {
}

public Person(String fullName, String jobTitle, String userName) {
    this.fullName = fullName;
    this.jobTitle = jobTitle;
    this.userName = userName;
}

我添加了适当的 getter 和 setter 以及 equals 方法。

但是,我在从该块的输入流中读取实体时遇到错误。

@Test
public void testPostPerson() throws Exception {
    final Person person = new Person("Dr. IntegrationTest", "Chief Wizard", "Dr. Wizard");
    final Person newPerson = RULE.client().target("http://localhost:" + RULE.getLocalPort() + "/people")
            .request()
            .post(Entity.entity(person, MediaType.APPLICATION_JSON_TYPE))
     -->    .readEntity(Person.class);
    assertThat(newPerson.getId()).isNotNull();
    assertThat(newPerson.getFullName()).isEqualTo(person.getFullName());
    assertThat(newPerson.getJobTitle()).isEqualTo(person.getJobTitle());
    assertThat(newPerson.getUserName()).isEqualTo(person.getUserName());
}

输入流错误的原因如下

    Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "code" (class com.example.helloworld.core.Person), not marked as ignorable (4 known properties: "fullName", "id", "userName", "jobTitle"])

@JsonIgnoreProperties 级别的 class 注释可以解决这个问题吗?这种做法安全吗?

编辑:PersonResource.java

@Path("/people/{personId}")
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {

private final PersonDAO peopleDAO;

public PersonResource(PersonDAO peopleDAO) {
    this.peopleDAO = peopleDAO;
}

@GET
@UnitOfWork
public Person getPerson(@PathParam("personId") LongParam personId) {
    return findSafely(personId.get());
}

@GET
@Path("/view_freemarker")
@UnitOfWork
@Produces(MediaType.TEXT_HTML)
public PersonView getPersonViewFreemarker(@PathParam("personId") LongParam personId) {
    return new PersonView(PersonView.Template.FREEMARKER, findSafely(personId.get()));
}

@GET
@Path("/view_mustache")
@UnitOfWork
@Produces(MediaType.TEXT_HTML)
public PersonView getPersonViewMustache(@PathParam("personId") LongParam personId) {
    return new PersonView(PersonView.Template.MUSTACHE, findSafely(personId.get()));
}

private Person findSafely(long personId) {
    return peopleDAO.findById(personId).orElseThrow(() -> new NotFoundException("No such user."));
}

我认为是因为资源失败抛出web应用异常,code实际上是http状态码

这样试试:

Response response = RULE.client().target("http://localhost:" + RULE.getLocalPort() + "/people")
        .request()
        .post(Entity.entity(person, MediaType.APPLICATION_JSON_TYPE));
assertEquals(200, response.getStatus());
Person newPerson = response.readEntity(Person.class);
....

你也可以这样调试:

String responseString = response.readEntity(String.class);

这将转储您的响应正文。