0
What I have to implement to provide a Webservice 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.
0
What I have to implement to provide a Webservice 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.
1
There are some frameworks that facilitate the development of webservices in java, one of the most popular is the 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);
}
The Spring
is a widely used and practical framework. A suggestion would be to consider the possibility of using Vraptor where the Controllers
are written in a cleaner way, ex:
@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:
Browser other questions tagged java web-service rest
You are not signed in. Login or sign up in order to post.
What have you tried? Please enter your code. You can explain why it has to be PUT and DELETE?
– Marconi
Just a moment I’ll test, I’m trying to get better here real quick, hold on.
– Aline
If you don’t know anything about, here can be a good walk to start.
– Marconi
What do you mean, kicking you out? rs
– Marconi