{
"id":"5894a12f-dae1-5ab0-5761-1371ba4f703e",
"title":"2017年的Spring发展方向",
"date":"2017-01-21",
"body":"这篇文章主要探讨如何利用Spring Boot集成NoSQL",
"createdBy":User,
"images":["http://dev.local/myfirstimage.png","http://dev.local/mysecondimage.png"],
"videos":[
{"url":"http://dev.local/myfirstvideo.mp4", "title":"The first video"},
{"url":"http://dev.local/mysecondvideo.mp4", "title":"The second video"}
],
"audios":[
{"url":"http://dev.local/myfirstaudio.mp3", "title":"The first audio"},
{"url":"http://dev.local/mysecondaudio.mp3", "title":"The second audio"}
]
}
package dev.local.todo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "todo", path = "todo")
public interface TodoRepository extends MongoRepository<Todo, String>{
}
此时我们甚至不需要Controller了,所以暂时注释掉 TodoController.java 中的代码。然后我们 ./gradlew bootRun 启动应用。访问 http://localhost:8080/todo 我们会得到下面的的结果。
{
_embedded: {
todo: [ ]
},
_links: {
self: {
href: "http://localhost:8080/todo"
},
profile: {
href: "http://localhost:8080/profile/todo"
}
},
page: {
size: 20,
totalElements: 0,
totalPages: 0,
number: 0
}
}
{
"alps" : {
"version" : "1.0",
"descriptors" : [
{
"id" : "todo-representation",
"href" : "http://localhost:8080/profile/todo",
"descriptors" : [
{
"name" : "desc",
"type" : "SEMANTIC"
},
{
"name" : "completed",
"type" : "SEMANTIC"
}
]
},
{
"id" : "create-todo",
"name" : "todo",
"type" : "UNSAFE",
"rt" : "#todo-representation"
},
{
"id" : "get-todo",
"name" : "todo",
"type" : "SAFE",
"rt" : "#todo-representation",
"descriptors" : [
{
"name" : "page",
"doc" : {
"value" : "The page to return.",
"format" : "TEXT"
},
"type" : "SEMANTIC"
},
{
"name" : "size",
"doc" : {
"value" : "The size of the page to return.",
"format" : "TEXT"
},
"type" : "SEMANTIC"
},
{
"name" : "sort",
"doc" : {
"value" : "The sorting criteria to use to calculate the content of the page.",
"format" : "TEXT"
},
"type" : "SEMANTIC"
}
]
},
{
"id" : "patch-todo",
"name" : "todo",
"type" : "UNSAFE",
"rt" : "#todo-representation"
},
{
"id" : "update-todo",
"name" : "todo",
"type" : "IDEMPOTENT",
"rt" : "#todo-representation"
},
{
"id" : "delete-todo",
"name" : "todo",
"type" : "IDEMPOTENT",
"rt" : "#todo-representation"
},
{
"id" : "get-todo",
"name" : "todo",
"type" : "SAFE",
"rt" : "#todo-representation"
}
]
}
}
{
"id" : "get-todo",
"name" : "todo",
"type" : "SAFE",
"rt" : "#todo-representation"
}
@RepositoryRestResource(collectionResourceRel = "todo", path = "todo")
public interface TodoRepository extends MongoRepository<Todo, String>{
}
public class SimpleMongoRepository<T, ID extends Serializable> implements MongoRepository<T, ID> {
private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation;
/**
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
*
* @param metadata must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
*/
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
Assert.notNull(mongoOperations);
Assert.notNull(metadata);
this.entityInformation = metadata;
this.mongoOperations = mongoOperations;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null!");
if (entityInformation.isNew(entity)) {
mongoOperations.insert(entity, entityInformation.getCollectionName());
} else {
mongoOperations.save(entity, entityInformation.getCollectionName());
}
return entity;
}
...
public T findOne(ID id) {
Assert.notNull(id, "The given id must not be null!");
return mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName());
}
private Query getIdQuery(Object id) {
return new Query(getIdCriteria(id));
}
private Criteria getIdCriteria(Object id) {
return where(entityInformation.getIdAttribute()).is(id);
}
...
}
private static final String QUERY_PATTERN = "find|read|get|query|stream";
List<Todo> findByCompleted(@Param("completed") boolean completed);
package dev.local.user;
import org.springframework.data.annotation.Id;
public class User {
@Id private String id;
private String username;
private String email;
//此处为节省篇幅省略属性的getter和setter
}
package dev.local.user;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
}
package dev.local.todo;
//省略import部分
public class Todo {
//省略其他部分
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
public interface TodoRepository extends MongoRepository<Todo, String>{
List<Todo> findByUserEmail(@Param("userEmail") String userEmail);
}
{
"_embedded" : {
"todos" : [ {
"desc" : "go swimming",
"completed" : false,
"user" : {
"username" : "peng",
"email" : "peng@gmail.com"
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/58908a92c5d0e2524e24545a"
},
"todo" : {
"href" : "http://localhost:8080/todos/58908a92c5d0e2524e24545a"
}
}
}, {
"desc" : "go for walk",
"completed" : false,
"user" : {
"username" : "peng",
"email" : "peng@gmail.com"
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/58908aa1c5d0e2524e24545b"
},
"todo" : {
"href" : "http://localhost:8080/todos/58908aa1c5d0e2524e24545b"
}
}
}, {
"desc" : "have lunch",
"completed" : false,
"user" : {
"username" : "peng",
"email" : "peng@gmail.com"
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/58908ab6c5d0e2524e24545c"
},
"todo" : {
"href" : "http://localhost:8080/todos/58908ab6c5d0e2524e24545c"
}
}
}, {
"desc" : "have dinner",
"completed" : false,
"user" : {
"username" : "peng",
"email" : "peng@gmail.com"
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/58908abdc5d0e2524e24545d"
},
"todo" : {
"href" : "http://localhost:8080/todos/58908abdc5d0e2524e24545d"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/search/findByUserEmail?userEmail=peng@gmail.com"
}
}
}
public interface TodoRepository extends MongoRepository<Todo, String>{
List<Todo> findByUserId(@Param("userId") String userId);
}
package dev.local.todo;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.List;
@RepositoryRestResource(collectionResourceRel = "todos", path = "todos")
public interface TodoRepository extends MongoRepository<Todo, String>{
List<Todo> findByUserId(@Param("userId") ObjectId userId);
}
List<Todo> findByUserIdAndDescLike(@Param("userId") ObjectId userId, @Param("desc") String desc);
public interface TodoRepository extends MongoRepository<Todo, String>{
@Query("{ 'user._id': ?0, 'desc': { '$regex': ?1} }")
List<Todo> searchTodos(@Param("userId") ObjectId userId, @Param("desc") String desc);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有