2016-09-29 59 views
0

我试着在网上寻找使用放置休息方法的例子,它类似于github上的dropwizard例子,但我还没有真正找到任何东西。我们如何使用dropwizard实现放置操作

这里是我的PersonDAO.java代码

public class PersonDAO extends AbstractDAO<Person> { 
    public PersonDAO(SessionFactory factory) { 
     super(factory); 
    } 
    public Optional<Person> findById(Long id) { 
     return Optional.fromNullable(get(id)); 
    } 

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

    public List<Person> findAll() { 
     return list(namedQuery("com.example.helloworld.core.Person.findAll")); 
    } 
    //here i want to create delete method 
} 

这里是我的REST API代码:

@Path("/people") 
@Produces(MediaType.APPLICATION_JSON) 
public class PeopleResource { 

    private final PersonDAO peopleDAO; 

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

    @POST 
    @UnitOfWork 
    public Person createPerson(Person person) { 
     return peopleDAO.create(person); 
    } 

    @GET 
    @UnitOfWork 
    public List<Person> listPeople() { 
     return peopleDAO.findAll(); 
    } 

    @GET 
    @Path("/{id}") 
    public Optional<Person> getUser(@PathParam("id") Integer id) { 
     Optional<Person> person = peopleDAO.findPeopleById(id); 
     return person; 
    } 
    //here should be put method that calls dao class 
} 

这里是我的person.java类

@Entity 
@Table(name = "people") 
@NamedQueries(
    { 
     @NamedQuery(
      name = "com.example.helloworld.core.Person.findAll", 
      query = "SELECT p FROM Person p" 
     ) 
    } 
) 
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; 

    public Person() { 
    } 

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

    public long getId() { 
     return id; 
    } 

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

    public String getFullName() { 
     return fullName; 
    } 

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

    public String getJobTitle() { 
     return jobTitle; 
    } 

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

    @Override 
    public boolean equals(Object o) { 
     if (this == o) { 
      return true; 
     } 
     if (!(o instanceof Person)) { 
      return false; 
     } 

     final Person that = (Person) o; 

     return Objects.equals(this.id, that.id) && 
       Objects.equals(this.fullName, that.fullName) && 
       Objects.equals(this.jobTitle, that.jobTitle); 
    } 

    @Override 
    public int hashCode() { 
     return Objects.hash(id, fullName, jobTitle); 
    } 
} 

有没有简单的方法/建议实施put方法?

+0

你的意思是要求放?您可以使用资源方法上方的@ Get,@ Post,@ Put和@ Delete注释来指定它支持的操作。你尝试时是否有错误? – Brion

回答

0

资源:

@PUT 
public Person update(Person person) { 
    return peopleDAO.update(person); 
} 

道:

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

确保您的ID不为空。坚持逻辑创建或更新。

+0

这会替换数据库中的条目吗?例如,如果我使用put请求和我的url在本地主机http:// localhost:8080/people/2中,其中两个是数据库的主ID,是否会更新数据库中的第二个条目? – Bob

+0

是的。如果你没有任何控制。对于这样的状态,我使用@Version注释列。 例如 – Hasan

+0

@版本 private long lastUpdated; – Hasan