在我使用soapui测试了一些简单的Web应用程序之后,我尝试使用@DELETE
请求。有了这个应用程序,我可以添加并获取用户/书籍到数据库。现在我试图提出@DELETE
请求,但我无法做到。这是代码:
//UserServiceImpl
@PersistenceContext
private EntityManager em;
@Override
public void deleteUser(Long id) {
if (null == id || id.longValue() < 1) {
throw new IllegalArgumentException(" User id can not be null or less than zero. ");
}
User u = em.find(User.class, id);
em.remove(u);
}
//UserResource
@Autowired
private UserService userService;
@DELETE
@Path("/delete/{id}")
public Response deleteUser(@PathParam("id") String id) {
Response response;
try {
User user = userService.deleteUser(Long.valueOf(id));//here is the error
if (user != null) {
response = Response.status(HttpServletResponse.SC_OK).entity(user).build();
} else {
response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
}
} catch (IllegalArgumentException e) {
response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
}
return response;
}
答案 0 :(得分:0)
我解决了我的问题。 UserServiceImpl中的delete方法不能为void ....它必须是public User deleteUser(Long id)。 Resource类中的另一个delete方法......只需要是void类型。我没有使用Response,我只是打印出这样的结果:
System.out.print(Response.status(HttpServletResponse.SC_OK).entity(user).build());