我试着在网上寻找使用放置休息方法的例子,它类似于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方法?
你的意思是要求放?您可以使用资源方法上方的@ Get,@ Post,@ Put和@ Delete注释来指定它支持的操作。你尝试时是否有错误? – Brion