有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我遵循本教程为我的数据库创建了一个RESTful api,以便在安卓中访问它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、music、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) 个答案

  1. # 1 楼答案

    响应标题以以下内容开头:

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

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

    再说一次,-g在这里是不需要的;)