Redirect to URL in java REST? or not redirect?

0

I understand that Rest is an architecture between a server and a client, httpRequest and httpResponse, it is the topic that we are seeing in a course that I am doing. The problem is that I'm doing in Rest a code that inserts data through parameters @FormParam in a database, which works excellent, however, once the data is sent I want it to be redirected to another page, for example the start but here my question comes, in my course they say no, I have to do something with html or javascript and the client makes decisions, I do not really understand that, I am so used to the servlets and the RequestDispatcher and redirect me that this I do not understand part of it.

I have followed recommendations like using URi or httpResponse.sendRelocation (); but they say it goes against the philosophy of REST.

I leave my code for you to see, I'm really stuck by the habit of redirection, how do I apply what they say the client makes decisions ???. As you will see the method Returns a String, however I thought about changing the method by a Response, but is that logical? Is that practice that I want to do in Rest wrong? What is the client making decisions? and how do I apply it?

package aeropuerto_empleados;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


import data_access_object.ConexionAeropuerto;



//Acá tengo que poner algún path? porque me está tirando error http 404
public class Administradores {

@POST
@Produces(MediaType.TEXT_HTML)
@Path("/administradores")
public String insertarEmpleado(@FormParam("nombre") String nombre , @FormParam("apellido") String apellido, @FormParam("usuario") String usuario, @FormParam("pass") String pass, @FormParam("email") String email, @FormParam("direccion") String direccion){

    try
        {

        ConexionAeropuerto c= new ConexionAeropuerto();
        Connection con= c.connectarAhora();

        if(con!=null)
                {
                    Statement st;
                    st=con.createStatement();
                    st.executeUpdate("INSERT INTO administradores(nombre,apellido,usuario,pass,email,direccion) VALUES('"+nombre+"','"+apellido+"','"+usuario+"','"+pass+"','"+email+"','"+direccion+"')");
                    st.close();
                    System.out.println("Funciona el try and catch");


                    //Qué codigo puedo escribir acá para que me redireccione al index.jsp una vez realizada la consulta??? debe ser simple imagino. 
                    //Response.getRequestDispatcher("index.jsp").forward(request, response);
                } 
        else
                {
                    System.out.println("Algo Salió mal no se pudo insertar los datos");
                }
        }
        catch (SQLException e) 

                {
                    e.printStackTrace();
                }

        return "<html>" + "<body><h3>" + "El nombre del empleado es: "  + usuario  + direccion + "<br>"+ "El apellido del empleado es: " + pass + "</h3></body>" + "</html>";
    }

}
    
asked by berlot83 06.03.2017 в 19:26
source

2 answers

1

what you have been told is correct. The REST technology is oriented to resources using http as protocol and the answers are always state values of http (200, 201, 400, ...). That does not mean you can use the headers to send additional information (apikeys ...).

Regarding your example, just tell you that the url, following the REST philosophy, is not well constructed since, as I mentioned before, they must represent resources and the http methods represent the actions with those resources. Therefore, the url for your service should be .../administradores/ and when doing a POST on it you create an administrator and by doing, for example, a GET on .../administradores/{keyadministrador} would return the administrator corresponding to that identifier. In addition, JSON or XML is used to exchange information, and HTML is never returned.

Having said that, regarding the issue of redirection, with a resource-based orientation, if you invoke an endpoint with POST ( .../administrador ) the service, after performing the corresponding operation, you should return a code 201. That tells the client that the resource has been created. And it is the client's responsibility to decide what to do next. In this case redirect to another page. Keep in mind that this is specific to each implementation, there will be times that you want to redirect, others will only show a message that the operation has been carried out correctly ... that depends on what the user is consuming your api rest wants to do afterwards .

I hope you have clarified things a bit with this tostón:)

    
answered by 07.03.2017 / 09:15
source
0

After almost 8 months of formulated by me this question I intend to add some data that I did not put in the question itself, the answer of @ fernando-forcén was very clear and it took away all the doubts about REST. However, there are some details that I want to clarify regarding my question.

1) The code of the question is based on jersey 1.19 if memory does not fail me.

2) I named classes like HttpRequest, HttpResponse and RequestDispatcher because at that time I was very aware of Servlets, however they have nothing to do with Rest for what I understand at the moment.

3) In the DB insertion code I used a Statement to make a query, contrary to what is stated in the Official Oracle Documentation which recommends using PreparedStatement, it worked for me, but it was not recommended. Today I have a little clearer things, I hope this second clarification helps someone.

4) As Fernando Forcén Rest said, I should return the answer in xml or json, in my case I started with the Jackson 2.8.0 class since it is the most popular, however over time I leaned over Gson because to my understanding is simpler and works spectacularly.

    
answered by 12.11.2017 в 19:59