Professional Documents
Culture Documents
Laboratorio 1 ONP
Laboratorio 1 ONP
1. Controller
package com.scotia.usows.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import com.scotia.usows.CustomErrorType;
import com.scotia.usows.model.User;
import com.scotia.usows.service.UserService;
@RestController
@RequestMapping("/api")
public class RestApiController {
@RequestMapping(value="/user",method=RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
logger.info("Mostrando todos los usuarios");
List<User> users = userService.consulta();
if(users.isEmpty()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<List<User>>(users,HttpStatus.OK);
@RequestMapping(value="/user/{id}", method=RequestMethod.GET)
public ResponseEntity<?> getUser(@PathVariable("id") long id){
logger.info("Buscando usuario con id de entrada {}",id);
User user =userService.consultarPorId(id);
if(user == null) {
logger.error("usuario con id {} no existe",id);
return new ResponseEntity(new CustomErrorType("Usuario con id
"+id+" no encontrado"),
HttpStatus.NOT_FOUND);
}
return new ResponseEntity<User>(user,HttpStatus.OK);
}
@RequestMapping(value="/user", method=RequestMethod.POST)
public ResponseEntity<?> createUser(@RequestBody User
user,UriComponentsBuilder ucBuilder){
@RequestMapping(value="/user/{id}",method= RequestMethod.PUT)
public ResponseEntity<?> updateUser(@PathVariable("id") long
id,@RequestBody User user){
if(suser == null) {
logger.error("Error en actualizacion: usuario con id {} no
existe",id);
return new ResponseEntity(new CustomErrorType("No se puede
actualzar usuario "+user.getId()+
" ,no existe"),HttpStatus.NOT_FOUND);
}
suser.setAge(user.getAge());
suser.setName(user.getName());
suser.setSalary(user.getSalary());
userService.registro(suser);
return new ResponseEntity<User>(suser,HttpStatus.OK);
}
}
}
2. Entity
package com.nh.rest.ejmrest;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private int age;
private double salary;
public User() {
// TODO Auto-generated constructor stub
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "User [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
--------------------------------------------------------------------------------
---------------------------------------------------------------------------
3. Interface CRUD
package com.nh.rest.ejmrest;
import org.springframework.data.repository.CrudRepository;
-----------------------------------------------------------------------------
4. Service
package com.nh.rest.ejmrest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserRepository userRepository;
5. ErrorType
package com.nh.rest.ejmrest;
---------------------------------------------------------------------------
6. Aplication
package com.nh.rest.ejmrest;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EjmrestApplication {
Ejecucion
http://localhost:8022/api/user.
En Postman
Consulta de un usuario especifico
http://localhost:8022/api/user/2
En postman
Uso de POST en Postman