@Delete返回HTTP状态405-不允许的方法

时间:2018-11-06 15:47:10

标签: java hibernate jpa java-ee nullpointerexception

嗨,我正在尝试从数据库中选择某些实体,但是当我使用@Delete时,在浏览器中会出现错误,但是Get可以正常工作。我正在使用休眠JPA

这是我的代码示例

@Entity

def VectorSpaceMatrix(files__, dict__):
assert  isinstance(files__, list)
assert  isinstance(dict__, dict)
termlist = []
filelist = []

for terms in dict__.keys():
    termlist.append(terms)
for fileid in files__:
    filelist.append(fileid)
sfl = SortFiles(filelist)
termlist.sort()

vecmat = np.zeros(((len(sfl)), (len(termlist))))

for row in enumerate(termlist):
    if row[1] in dict__.keys():
        for column in sfl:
            if column[1] in dict__[row[1]].keys():
                #pprint.pprint(dict__[row[1]][column[1]][4])
                tfidf = float(dict__.get(row[1]).get(column[1])[4])

                c_index = column[0]
                r_index = row[0]

                index = np.array([r_index, c_index])

                insert = np.ravel_multi_index(index.T, vecmat.shape)

                np.put(vecmat, insert, tfidf, mode='clip')
df = pd.DataFrame(vecmat, index=sfl, columns=termlist)
df.to_csv("df.csv", index=True, header=True, sep=' ')

presistance.xml

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;
    }
}

具有方法的存储库

<?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>

在这里我使用@Delete

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));
    }
}

我建议在Google Chrome中回复 Response

我将不胜感激:)预先感谢;)

2 个答案:

答案 0 :(得分:1)

您面临的问题是因为始终通过GET http方法访问浏览器URL。您无法使用其他http方法。

为了测试DELETE端点,您必须使用REST客户端来完成它。

其余客户端的一些示例:命令行:curlwget。使用GUI:PostmanInsomnia

从命令行执行此操作的示例:

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

答案 1 :(得分:0)

@Delete我在浏览器中遇到错误,但Get正常工作。

当您在浏览器中点击url时,它将作为GET请求。

除了通过浏览器执行GET之外,您无法发出其他任何请求,因此GET可以正常工作。

尝试使用http客户端工具(如邮递员等)或curl。