如何为 Spring 数据 JPA REST 启用 Post/Put 方法? ("error 405 : method not allowed")

how to Enable Post/Put Method for Spring data JPA REST ? ("error 405 : method not allowed")

我按照本教程为我的数据库创建了一个 RESTful api,以便在 android 中访问它。 https://spring.io/guides/gs/accessing-data-rest/

api 有效 我可以看到我的数据库数据格式化为 JSON 但是当我尝试 post 使用 curl 它根本不起作用,我得到一个错误:

</html>curl (6) Could not resolve host: 1
curl: (6) Could not resolve host: }'
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: HEAD, GET
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 26 Apr 2016 02:20:04 GMT

{"timestamp":1461637204513,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/musics/"}

我使用的命令:

curl -g -i -X POST -H "Content-Type:application/json" -d '{  "music" : "1"  }' http://localhost:8080/musics/

对于数据库字段: 音乐(musicID,音乐,added_at)

对于 POJO :

package com.lamssaweb.entities;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Date;
import java.util.Collection;

@Entity
@Table(name = "musics", schema = "", catalog = "scout")
public class MusicsEntity implements Serializable{
    private int musicid;
    private String music;
    private Date addedAt;
    private String description;
    private Collection<PostsEntity> postsesByMusicid;

    @Id
    @Column(name = "MUSICID", nullable = false, insertable = true, updatable = true)
    public int getMusicid() {
        return musicid;
    }

    public void setMusicid(int musicid) {
        this.musicid = musicid;
    }

    @Basic
    @Column(name = "MUSIC", nullable = true, insertable = true, updatable = true, length = 2000)
    public String getMusic() {
        return music;
    }

    public void setMusic(String music) {
        this.music = music;
    }

    @Basic
    @Column(name = "ADDED_AT", nullable = true, insertable = true, updatable = true)
    public Date getAddedAt() {
        return addedAt;
    }

    public void setAddedAt(Date addedAt) {
        this.addedAt = addedAt;
    }

    @Basic
    @Column(name = "DESCRIPTION", nullable = true, insertable = true, updatable = true, length = 2000)
    public String getDescription() {
        return description;
    }

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

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

        MusicsEntity that = (MusicsEntity) o;

        if (musicid != that.musicid) return false;
        if (addedAt != null ? !addedAt.equals(that.addedAt) : that.addedAt != null) return false;
        if (description != null ? !description.equals(that.description) : that.description != null) return false;
        if (music != null ? !music.equals(that.music) : that.music != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = musicid;
        result = 31 * result + (music != null ? music.hashCode() : 0);
        result = 31 * result + (addedAt != null ? addedAt.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

//    @OneToMany(mappedBy = "musicsByMusicid")
//    @JsonManagedReference
//    public Collection<PostsEntity> getPostsesByMusicid() {
//        return postsesByMusicid;
//    }
//
//    public void setPostsesByMusicid(Collection<PostsEntity> postsesByMusicid) {
//        this.postsesByMusicid = postsesByMusicid;
//    }
}

对于主要的:

@SpringBootApplication
public class SpringBackendScoutApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBackendScoutApplication.class, args);
    }
}

响应 header 开头为:

</html>curl (6) Could not resolve host: 1
curl: (6) Could not resolve host: }'

这与您 other post: you're on Windows so don't use single quotes, use double quotes and escape the inner double quotes 中的问题相同:

curl -g -i -X POST -H "Content-Type:application/json" -d "{ \"music\" : \"1\" }" http://localhost:8080/musics/

再一次,这里不需要 -g ;)