简体   繁体   中英

@Delete returns HTTP Status 405 - Method Not Allowed

Hi i am trying to delect some entities from database, but when i use @Delete i get error in browsers, but Get is working. I am using hibernate JPA

Here are my code samples

@Entity

package pl.test.model;

import javax.persistence.*;
import java.util.Collection;

@Entity
public class Mestechnologygroup {
    private Integer idTechnologyGroup;
    private String name;
    private String description;
    private Integer number;
    private Collection<Mestechnology> mestechnologiesByIdTechnologyGroup;

    @Id
    @Column(name = "idTechnologyGroup")
    public Integer getIdTechnologyGroup() {
        return idTechnologyGroup;
    }

    public void setIdTechnologyGroup(Integer idTechnologyGroup) {
        this.idTechnologyGroup = idTechnologyGroup;
    }

    @Basic
    @Column(name = "Name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "Description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Basic
    @Column(name = "Number")
    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Mestechnologygroup that = (Mestechnologygroup) o;

        if (idTechnologyGroup != null ? !idTechnologyGroup.equals(that.idTechnologyGroup) : that.idTechnologyGroup != null)
            return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (description != null ? !description.equals(that.description) : that.description != null) return false;
        if (number != null ? !number.equals(that.number) : that.number != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = idTechnologyGroup != null ? idTechnologyGroup.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (number != null ? number.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "mestechnologygroupByIdTechnologyGroup")
    public Collection<Mestechnology> getMestechnologiesByIdTechnologyGroup() {
        return mestechnologiesByIdTechnologyGroup;
    }

    public void setMestechnologiesByIdTechnologyGroup(Collection<Mestechnology> mestechnologiesByIdTechnologyGroup) {
        this.mestechnologiesByIdTechnologyGroup = mestechnologiesByIdTechnologyGroup;
    }
}

presistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="testPU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>pl.test.model.Mesattachmentoperation</class>
        <class>pl.test.model.Mesattachmenttechnology</class>
        <class>pl.test.model.Mesoperation</class>
        <class>pl.test.model.Mesoperationdictionary</class>
        <class>pl.test.model.Mesoperationstate</class>
        <class>pl.test.model.Mesproduct</class>
        <class>pl.test.model.Mesproducttype</class>
        <class>pl.test.model.Mesproductxoperation</class>
        <class>pl.test.model.Mesresource</class>
        <class>pl.test.model.Mesresourcexoperation</class>
        <class>pl.test.model.Mestechnology</class>
        <class>pl.test.model.Mestechnologygroup</class>
        <class>pl.test.model.Mesusers</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mes"/>
            <property name="hibernate.connection.username" value="postgres"/>
            <property name="hibernate.connection.password" value="xxxx"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>

        </properties>
    </persistence-unit>
</persistence>

Repository with method

package pl.test.repo;

import com.sun.istack.internal.NotNull;
import pl.test.model.Mestechnologygroup;
import pl.test.model.Mesusers;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import java.util.List;

import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;

@Transactional(SUPPORTS)
public class TechnologyGroupRepo {

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testPU");
    EntityManager em = entityManagerFactory.createEntityManager();

    public Mestechnologygroup find(@NotNull Integer id) {
        return em.find(Mestechnologygroup.class, id);
    }

    public List<Mestechnologygroup> findAll() {
        TypedQuery<Mestechnologygroup> query = em.createQuery("from Mestechnologygroup ", Mestechnologygroup.class);
        return query.getResultList();
    }

    @Transactional(REQUIRED)
    public void delete(@NotNull Integer id) {
        em.remove(em.getReference(Mestechnologygroup.class, id));
    }
}

here i use @Delete

package pl.test.rest;


import pl.test.model.Mestechnologygroup;
import pl.test.repo.TechnologyGroupRepo;

import javax.inject.Inject;
import javax.validation.constraints.Min;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import java.util.List;

import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@Path("/tg")
public class TechnologyGroupEndpoint{

        @Inject
        private TechnologyGroupRepo technologyGroupRepo;

        @GET
        @Path("/{id : \\d+}")
        @Produces(APPLICATION_JSON)
        public Response getBook(@PathParam("id") @Min(1) Integer id) {
            Mestechnologygroup mestechnologygroup = technologyGroupRepo.find(id);

            if (mestechnologygroup == null)
                return Response.status(Response.Status.NOT_FOUND).build();

            return Response.ok(mestechnologygroup).build();
        }

        @DELETE
        @Path("/d/{id : \\d+}")
        public Response deleteBook(@PathParam("id") @Min(1) Integer id) {
            technologyGroupRepo.delete(id);
            return Response.noContent().build();
        }

        @GET
        @Produces(APPLICATION_JSON)
        public Response getBooks() {
            List<Mestechnologygroup> mestechnologygroups = technologyGroupRepo.findAll();

            if (mestechnologygroups.size() == 0)
                return Response.status(Response.Status.NO_CONTENT).build();

            return Response.ok(mestechnologygroups).build();
        }


}

I revice that response in Google chrome Response

I would appreciate any help :) Thanks in advance;)

The problem that you are facing is because a browser url is always accessed via GET http method. You cannot do for other http methods.

In order to test your DELETE endpoint, you have to do it using a REST client.

A few examples of rest clients: command line: curl , wget . With GUI: Postman , Insomnia .

An example of doing this from the command line:

curl -X DELETE "http://localhost:8080/test-1.0-SNAPSHOT/resources/tg/d/22"

@Delete i get error in browsers, but Get is working.

When you hit url on browser, it takes as GET request.

you can not make any other request than GET by browser, so GET works.

Try using http client tool like postman etc. or curl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM