Şu anda Spring Data REST kullanan bir Spring Boot uygulamam var. Bir etki alanı varlık var Postsahiptir @OneToMany, başka bir etki alanı varlığa ilişkiyi Comment. Bu sınıflar aşağıdaki şekilde yapılandırılmıştır:
Post.java:
@Entity
public class Post {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
private String title;
@OneToMany
private List<Comment> comments;
// Standard getters and setters...
}
Comment.java:
@Entity
public class Comment {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
@ManyToOne
private Post post;
// Standard getters and setters...
}
Spring Data REST JPA depoları şunların temel uygulamalarıdır CrudRepository:
PostRepository.java:
public interface PostRepository extends CrudRepository<Post, Long> { }
CommentRepository.java:
public interface CommentRepository extends CrudRepository<Comment, Long> { }
Uygulama giriş noktası, standart, basit bir Spring Boot uygulamasıdır. Her şey yapılandırılmış stok.
Application.java
@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
Görünüşe göre her şey doğru çalışıyor. Uygulamayı çalıştırdığımda, her şey düzgün çalışıyor gibi görünüyor. Beğenmek için yeni bir Gönderi nesnesi POST yapabilirim http://localhost:8080/posts:
Vücut:
{"author":"testAuthor", "title":"test", "content":"hello world"}
Sonuç http://localhost:8080/posts/1:
{
"author": "testAuthor",
"content": "hello world",
"title": "test",
"_links": {
"self": {
"href": "http://localhost:8080/posts/1"
},
"comments": {
"href": "http://localhost:8080/posts/1/comments"
}
}
}
Ancak, bir GET gerçekleştirdiğimde , dönen http://localhost:8080/posts/1/commentsboş bir nesne {}alıyorum ve aynı URI'ye bir yorum göndermeye çalışırsam, HTTP 405 Yöntemine İzin Verilmiyor.
Bir Commentkaynak oluşturmanın ve bununla ilişkilendirmenin doğru yolu nedir Post? Mümkünse doğrudan adresine POST göndermekten kaçınmak istiyorum http://localhost:8080/comments.