目前我有一个使用Spring Data REST的Spring Boot应用程序.我有一个与另一个域实体Post有@OneToMany关系的域实体Comment.这些类的结构如下:
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...
}
Run Code Online (Sandbox Code Playgroud)
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...
}
Run Code Online (Sandbox Code Playgroud)
他们的Spring Data REST JPA存储库是以下基本实现CrudRepository:
PostRepository.java:
public interface PostRepository extends CrudRepository<Post, Long> …Run Code Online (Sandbox Code Playgroud) 我一直在研究如何使用Spring和REST的各种示例.我们的最终目标是Spring HATEOAS/HAL设置
我已经看到了两种在Spring中呈现REST的不同方法
通过HATEOAS/HAL控制器内部
通过@RestController存储库中的内容
我正在努力寻找的是你为什么要使用一个而不是另一个.在尝试实施HAL时哪个最好?
我们的数据库后端是Neo4j.
spring spring-mvc spring-data spring-data-rest spring-hateoas
我正在开发一个带有Rest接口和dart前端的Spring Boot应用程序.
XMLHttpRequest确实执行一个完全正确处理的OPTIONS请求.在此之后,发出最终的GET("/ products")请求并失败:
请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:63343 ",因此没有允许访问.
经过一些调试后,我发现了以下内容:为RepositoryRestHandlerMapping之外的所有子类填充了AbstractHandlerMapping.corsConfiguration.在RepositoryRestHandlerMapping中,没有corsConfiguration在创建时存在/设置,因此它不会被识别为cors路径/资源.
=>没有连接CORS标头
这可能是问题吗?我怎么设置它?
配置类:
@Configuration
public class RestConfiguration extends RepositoryRestMvcConfiguration {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowCredentials(false).allowedOrigins("*").allowedMethods("PUT", "POST", "GET", "OPTIONS", "DELETE").exposedHeaders("Authorization", "Content-Type");
}
...
}
Run Code Online (Sandbox Code Playgroud)
我甚至尝试设置每个注释的Cors:
@CrossOrigin( methods = RequestMethod.GET, allowCredentials = "false")
public interface ProductRepository extends CrudRepository<Product, String> {
}
Run Code Online (Sandbox Code Playgroud)
原始请求标头:
GET /products HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
authorization: Basic dXNlcjpwYXNzd29yZA==
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.130 Chrome/43.0.2357.130 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: …Run Code Online (Sandbox Code Playgroud) 我有一个实体如下
Class Person{
String id;
String name;
String numberOfHands;
}
Run Code Online (Sandbox Code Playgroud)
使用Spring Data Rest(Gosling Release Train),我可以指定
localhost/Person?sort=name,asc
Run Code Online (Sandbox Code Playgroud)
用于排序名称升序.现在,在我需要按numberOfHands降序排序并命名升序的情况下.我可以指定
localhost/Person?sort=numberOfHands,name,asc
Run Code Online (Sandbox Code Playgroud)
但是,我无法指定
localhost/Person?sort=numberOfHands,desc,name,asc
Run Code Online (Sandbox Code Playgroud)
有没有办法指定多个排序顺序?
谢谢!
我有一个扩展的通用Spring Data存储库接口QuerydslBinderCustomizer,允许我自定义查询执行.我正在尝试将内置的基本相等性测试扩展到默认存储库实现中,以便我可以使用Spring Data REST执行其他查询操作.例如:
GET /api/persons?name=Joe%20Smith // This works by default
GET /api/persons?nameEndsWith=Smith // This requires custom parameter binding.
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我创建的实体路径的每个别名似乎都覆盖了前面的别名绑定.
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable>
extends PagingAndSortingRepository<T, ID>, QueryDslPredicateExecutor<T>, QuerydslBinderCustomizer {
@Override
@SuppressWarnings("unchecked")
default void customize(QuerydslBindings bindings, EntityPath entityPath){
Class<T> model = entityPath.getType();
Path<T> root = entityPath.getRoot();
for (Field field: model.getDeclaredFields()){
if (field.isSynthetic()) continue;
Class<?> fieldType = field.getType();
if (fieldType.isAssignableFrom(String.class)){
// This binding works by itself, but not after the next one is added
bindings.bind(Expressions.stringPath(root, field.getName())) …Run Code Online (Sandbox Code Playgroud) 问题是,Spring HATEOAS与Spring Data Rest之间有什么区别?
我觉得两者都可以这样做,Spring Data Rest(作为Spring Data的一部分)看起来更有活力.
https://github.com/spring-projects/spring-hateoas https://github.com/spring-projects/spring-data-rest
你什么时候使用其中一个?
在版本2.0.2.RELEASE中使用带有JPA的Spring Data REST.
如何在JSON中禁用超文本应用程序语言(HAL)?http://stateless.co/hal_specification.html
我已经尝试了很多东西,但无济于事.例如,我已将Accept和Content-type标头设置为"application/json"而不是"application/hal + json",但我仍然收到带有超链接的JSON内容.
例如,我想得到类似的东西:
{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" : "Munchen",
"country" : "Germany",
"phone" : "+34 4410122000",
"vat" : "000000001",
"employees" : 225,
"sector" : {
"description" : "Marketing",
"average profit": 545656665,
"average employees": 75,
"average profit per employee": 4556
}
}
Run Code Online (Sandbox Code Playgroud)
代替:
{
"name" : "Foo",
"street" : "street Bar",
"streetNumber" : 2,
"streetLetter" : "b",
"postCode" : "D-1253",
"town" …Run Code Online (Sandbox Code Playgroud) 当我点击数据库时,PagingAndSortingRepository.findAll(Pageable)我得到了Page<ObjectEntity>.但是,我想将DTO暴露给客户端而不是实体.我可以通过将实体注入到它的构造函数中来创建DTO,但是如何将Page对象中的实体映射到DTO?根据spring文档,Page提供了只读操作.
另外,Page.map不可能,因为我们不支持java 8.如何手动创建带有映射对象的新页面?
我使用spring-data-rest将实体公开为(分页)休息资源.一切正常,但当我通过请求数据时RestTemplate,我得到一个无用的HATEOAS JSON(我没有要求).JSON似乎是一个PagedResources.我可以忍受,但JSON没有正确转换为对象.content里面没有.
库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>
{
List<Person> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)
客户:
public List<Person> getPersons()
{
RestTemplate rt = new RestTemplate();
System.out.println(rt.getForObject(URL, PagedResources.class).getContent().size());
System.out.println(rt.getForObject(URL, PagedResources.class).getLinks().size());
System.out.println(rt.getForObject(URL, PagedResources.class).getMetadata().getTotalElements());
return new ArrayList<Person>(rt.getForObject(URL, PagedResources.class).getContent()); // <-- empty
}
Run Code Online (Sandbox Code Playgroud)
System.out的:
0 // getContent().size()
4 // getLinks().size()
2 // getTotalElements()
Run Code Online (Sandbox Code Playgroud)
卷曲:
C:\...>curl http://localhost:8080/spring-jsf-rest/rest/people
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/spring-jsf-rest/rest/people{?page,size,sort}",
"templated" : true
},
"search" : {
"href" …Run Code Online (Sandbox Code Playgroud) 我试图让Kotlin在spring-data-rest项目上使用jsr 303验证.
给出以下数据类声明:
@Entity data class User(
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
var id: Long? = null,
@Size(min=5, max=15)
val name: String
)
Run Code Online (Sandbox Code Playgroud)
@Size注释在这里没有任何效果,使我能够保存名称为1个字符的用户.
它在执行相同的示例时效果很好,但在Java类而不是Kotlin中.
这让我想起了Kotlin问题.
在此先感谢您的帮助!
spring-data-rest ×10
spring ×8
spring-data ×6
java ×5
spring-mvc ×3
json ×2
rest ×2
cors ×1
data-class ×1
hateoas ×1
jpa ×1
kotlin ×1
querydsl ×1
spring-boot ×1