如何为Spring数据JPA REST启用Post / Put方法? (“错误405:方法不允许”)

时间:2016-04-26 02:34:36

标签: java spring curl spring-data spring-data-rest

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

api工作我可以看到我的数据库的数据格式化为JSON但是当我尝试使用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);
    }
}

1 个答案:

答案 0 :(得分:2)

响应标头以:

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

这与您的other post中的问题相同:您在Windows上don't use single quotes, use double quotes and escape the inner double quotes {/ 3>:

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

同样,这里不需要 -g ;)