2016-04-26 29 views
1

我按照本教程爲我的數據庫創建一個RESTful api來在android中訪問它。 https://spring.io/guides/gs/accessing-data-rest/如何爲Spring數據JPA REST啓用Post/Put方法? (「錯誤405:方法不允許」)

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

對於數據庫字段: 的音樂(音樂ID,音樂,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); 
    } 
} 

回答

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

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

再次,-g這裏不需要;)

+0

非常感謝你我測試它,它的工作原理! –