0

Im trying to insert and object "Persona" in my DB, but it dosnt work.

This is my Server

@PUT
@Path("/personas")
@Consumes(MediaType.APPLICATION_XML)
public Response addpelicula(Personas persona) {
    SessionFactory sesion = HibernateUtil.getSessionFactory();
    Session session = sesion.openSession();
    Transaction tx = session.beginTransaction();
    Response res;

    try {
        session.save(persona);
        tx.commit();
        res = Response.created(uriInfo.getAbsolutePath()).build();
    } catch (HibernateException e) {
        res = Response.noContent().build();
    } finally {
        session.close();
    }

    return res;
}

And this is my Client

public static void insertarPersona(String nombre, String apellidos, String pais) {
    try {
        /*
        * INSERTAR UNA PERSONA
         */
        direccionRest = "http://localhost:8080/ServerREST_WebAPP/rest/personas";

        Persona personaNueva = new Persona();
        personaNueva.setNombre(nombre);
        personaNueva.setApellidos(apellidos);
        personaNueva.setPais(pais);
        //personaNueva.setPeliculasList(peliculasList);

        jaxbContext = JAXBContext.newInstance(Pelicula.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        url = new URL(direccionRest);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
        conn.setDoOutput(true);

        jaxbMarshaller.marshal(personaNueva, conn.getOutputStream());

        if (conn.getResponseCode() != 201) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        System.out.println(":::---> Persona introducida: " + personaNueva.getNombre());

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The console show this error:

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 405 at clienterest_webapp.ClienteREST_WebAPP.insertarPelicula(ClienteREST_WebAPP.java:257) at clienterest_webapp.ClienteREST_WebAPP.main(ClienteREST_WebAPP.java:393) C:\Users\Usuario 2 DAM\Documents\ClienteREST_WebAPP\nbproject\build-impl.xml:1066: The following error occurred while executing this line: C:\Users\Usuario 2 DAM\Documents\ClienteREST_WebAPP\nbproject\build-impl.xml:831: Java returned: 1 BUILD FAILED (total time: 3 seconds)

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • The error occurs in `insertarPelicula` , please add the code of this method . – Arnaud Feb 22 '18 at 10:10
  • Check the response, allowed methods: https://stackoverflow.com/questions/21706078/put-and-post-getting-405-method-not-allowed-error-for-restful-web-services – Nevermore Feb 22 '18 at 10:11

0 Answers0