Dropwizard提出了请求问题

时间:2016-09-29 16:33:25

标签: java dropwizard

我在github上使用dropwizard示例,我在执行put请求时遇到问题。

我在Person.java类中有一个更新sql语句

"@NamedQuery(name = "com.example.helloworld.core.Person.update",query = "UPDATE Person p SET p.start=:start, p.end=:end where p.id=:id")"

在我的PersonDAO.java类中,我有一个名为update

的方法
public  List<Person>  update(Long id, Person person) {
return list(namedQuery("com.example.helloworld.core.Person.update").setParameter("id",id).setParameter("start",person.getStart())
.setParameter("end",person.getEnd()));}

在PersonResource.java下,我有@put方法,

@PUT
@UnitOfWork
public List<Person> updatePerson(@PathParam("personId") LongParam personId,Person person) {
   return peopleDAO.update(personId.get(),person);
}

当我测试我的put方法时,我收到此错误

INFO   [16:05:33.430] [dw-49 - PUT /people/2] o.h.e.i.StatisticalLoggingSessionEventListener -  Session Metrics {
60159 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)} 
ERROR  [16:05:33.431] [dw-49 - PUT /people/2] i.d.j.e.LoggingExceptionMapper -  Error handling a request: e27fe462c112d5ca 
java.lang.NullPointerException: null
at com.example.helloworld.db.PersonDAO.update(PersonDAO.java:27)
at com.example.helloworld.resources.PersonResource.updatePerson(PersonResource.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

我想知道是否有关于如何做到这一点的任何提示。或者有人可以提供一些关于如何在dropwizard示例中实现put方法的提示。

编辑:

PersonDAO.java

public class PersonDAO extends AbstractDAO<Person> {


public PersonDAO(SessionFactory factory) {
    super(factory);
}

public Optional<Person> findById(Long id) {
    return Optional.ofNullable(get(id));
}   

public Person create(Person person) {
    return persist(person);
}

public  List<Person>  update(Long id, Person person) {
  return  list(namedQuery("com.example.helloworld.core.Person.update").setParameter("id",id)
                                                                      .setParameter("start",person.getStart())
                                                                      .setParameter("end",person.getEnd()));
}
public List<Person> findAll() {
    return list(namedQuery("com.example.helloworld.core.Person.findAll"));
}
}

PersonResource.java

  @Path("/people/{personId}")
    @Produces(MediaType.APPLICATION_JSON)
    public class PersonResource {
    private static final Logger LOGGER = LoggerFactory.getLogger(PersonResource.class);
    public final PersonDAO peopleDAO;

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

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


    @PUT
    @UnitOfWork
    public List<Person> updatePerson(@PathParam("personId") LongParam personId,Person person) { 
     return peopleDAO.update(personId.get(),person);
    }

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

Person.java

@Entity
@Table(name = "people")
@NamedQueries({
        @NamedQuery(
            name = "com.example.helloworld.core.Person.findAll",
            query = "SELECT p FROM Person p"
        ),
        @NamedQuery(
            name = "com.example.helloworld.core.Person.update",
            query = "UPDATE Person p SET p.start=:start, p.end=:end where p.id=:id"
        )
    })
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

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

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

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

    @Column(name = "start", nullable = false)
    private int start;

    @Column(name = "end", nullable = false)
    private int end;

    public Person() {
    }

    public Person(String name, String day, int start, int end, String weekstartdate) {
        this.name = name;
        this.day= day;
    this.end = end;
    this.weekstartdate=weekstartdate;
    this.start = start;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
   public String getName() {
        return name;
    }

    public void setWeekStartDate(String weekstartdate) {
        this.weekstartdate = weekstartdate;
    }

    public String getWeekStartDate() {
        return weekstartdate;
    }

    public void setFullName(String fullName) {
        this.name = fullName;
    }

    public String getDay() {
        return day;
    }

    public void setJobTitle(String day) {
        this.day = day;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}

编辑2:

我正在使用邮递员,我使用put请求和网址发送一个人json:http://localhost:8080/people/5

{"name":"bob man","day":"Mon","start":2,"end":175,"weekStartDate":"19-9-2016"}

0 个答案:

没有答案