What do I have to implement to make a Webservice available that supports PUT or DELETE.
I'm new to this webservice world if you could help me by sending me at least one link from where I can read something about.
What do I have to implement to make a Webservice available that supports PUT or DELETE.
I'm new to this webservice world if you could help me by sending me at least one link from where I can read something about.
There are some frameworks that make it easy to develop webservices in java, one of the most popular is Spring .
Example of PUT
and DELETE
with Spring
:
@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
//busca um usuário por id(Parametro passado pro método updateUser)
User currentUser = userService.findById(id);
if (currentUser==null) {
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
currentUser.setName(user.getName());
currentUser.setAge(user.getAge());
currentUser.setSalary(user.getSalary());
//Faz o update do usuário no banco
userService.updateUser(currentUser);
//Retorna o usuário junto com código 200
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
//Busca um usuário por id(passado por parâmetro)
User user = userService.findById(id);
if (user == null) {
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
//Deleta o usuário
userService.deleteUser(user);
return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
}
Spring
is a widely used and very practical framework. One suggestion would be to consider using Vraptor where Controllers
are written more cleanly, eg:
@Controller
public class ClienteController {
@Post("/cliente")
public void adiciona(Cliente cliente) {...}
@Path("/")
public List<Cliente> lista() {
return ...
}
@Get("/cliente")
public Cliente visualiza(Cliente cliente) {
return ...
}
@Get("/cliente/{codigo}")
public Cliente listarPorCodigo(Long codigo) {
return ...
}
@Delete("/cliente")
public void remove(Cliente cliente) { ... }
@Put("/cliente")
public void atualiza(Cliente cliente) { ... }
}
Materials: