我试着在网上查找使用put rest方法的例子,该方法类似于github上的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
}
这是我的休息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 :(得分:0)
资源:
1156
['xyxyxyx', 'xyxyxyy', 'xyxyyxy', 'xyxyyyx', 'xyxyyyy', 'xyyxyxy', 'xyyxyyx', 'xyyxyyy', 'xyyyxyx', 'xyyyxyy', 'xyyyyxy', 'xyyyyyx', 'xyyyyyy', 'yxyxyxy', 'yxyxyyx', 'yxyxyyy', 'yxyyxyx', 'yxyyxyy', 'yxyyyxy', 'yxyyyyx', 'yxyyyyy', 'yyxyxyx', 'yyxyxyy', 'yyxyyxy', 'yyxyyyx', 'yyxyyyy', 'yyyxyxy', 'yyyxyyx', 'yyyxyyy', 'yyyyxyx', 'yyyyxyy', 'yyyyyxy', 'yyyyyyx', 'yyyyyyy']
dao:
@PUT
public Person update(Person person) {
return peopleDAO.update(person);
}
确保您的ID不为空。坚持逻辑创建或更新。