SHARE
TWEET

JSONApiPersonaRESTClientWeb/PersonaServlet.java/post

apuntesdejava Jun 8th, 2013 2,911 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @WebServlet(name = "PersonaServlet", urlPatterns = {"/PersonaServlet"})
  2. public class PersonaServlet extends HttpServlet {
  3.  
  4.     private static final String URL = "http://localhost:8080/PersonaRESTWeb/webresources/personas";
  5.     static final Logger LOGGER = Logger.getLogger(PersonaServlet.class.getName());
  6.  
  7.     @Override
  8.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  9.             throws ServletException, IOException {
  10.         LOGGER.info("Procesando peticion");
  11.         //obtenemos los parametros
  12.         String nombre = request.getParameter("name");
  13.         String $worker = request.getParameter("worker");
  14.         String $sexo = request.getParameter("gender");
  15.         //creamos el objeto..
  16.         Person p = new Person(nombre, //tomamos el nombre
  17.                 "S".equals($worker), //... si es igual "S" entonces es TRUE
  18.                 $sexo != null && !$sexo.isEmpty() ? $sexo.substring(0, 1) : null); // y colocamos el primer caracter, sino, NULL
  19.  
  20.         Client client = ClientBuilder.newClient(); //creamos el cliente RESt.. totalmente simple
  21.         client.register(PersonWriter.class); //Registramos nuestro interprete de Bean -> JSON
  22.         WebTarget target = client.target(URL);
  23.         LOGGER.info("Enviando al Servicio REST");
  24.         target.request()
  25.                 .post(Entity.entity(p, MediaType.APPLICATION_JSON), Person.class); //y hacemos post del objeto
  26.         response.sendRedirect("index.jsp"); //redireccionamos el control a /index.jsp
  27.     }
  28. //...
  29.  
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top