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

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

最佳答案

响应 header 开头为:

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

关于java - 如何为 Spring 数据 JPA REST 启用 Post/Put 方法? ("error 405 : method not allowed"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854326/

相关文章:

PHP curl,保留 session

俄语的php问题

ruby - 用于 curl/libcurl 的最佳 ruby 绑定(bind)/gem

java - Spring Framework 表单验证、Hibernate bean 和 bean 填充

java - Spring Data JPA QueryDslPredicateExecutor 找到不同的结果

java - 如何构建具有网页之间共享功能的 Spring Controller

java - 文件之间的克隆表

java - MongoDB 奇怪的行为,orOperator

java - 全屏图像上的 ViewFlipper

java - 阻止程序执行,直到用户单击按钮