lin-jinwei
注意,未授权不得擅自以盈利方式转载本博客任何文章。
Code: ../code/S6-dsw-mongodb-rest
代码:com/jinwei/S6_dsw_mongodb_rest/Connector.java
package com.jinwei.S6_dsw_mongodb_rest;
import org.springframework.data.annotation.Id;
public class Connector {
@Id private String id;
// 定义实例类需要 set-get 的各种属性
private String cacert;
private String description;
private String sellDataID;
private String buyDataID;
// get and set: cacert
public String getCacert() {
return cacert;
}
public void setCacert(String cacert) {
this.cacert = cacert;
}
// get and set: description
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// get and set: sellDataID
public String getSellDataID() {
return sellDataID;
}
public void setSellDataID(String sellDataID) {
this.sellDataID = sellDataID;
}
// get and set: buyDataID
public String getDuyDataID() {
return buyDataID;
}
public void setDuyDataID(String buyDataID) {
this.buyDataID = buyDataID;
}
}
代码:com/jinwei/S6_dsw_mongodb_rest/ConnectorRepository.java
package com.jinwei.S6_dsw_mongodb_rest;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface ConnectorRepository extends MongoRepository<Connector, String> {
List<Connector> findByLastName(@Param("name") String name);
}
运行成功:
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'com.jinwei'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
C:\Users\user>curl http://localhost:8080
{
"_links" : {
"cadata" : {
"href" : "http://localhost:8080/cadata{?page,size,sort*}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}
C:\Users\user>curl http://localhost:8080/cadata
{
"_embedded" : {
"cadata" : [ {
"cacert" : "cacert ca0001 class1 0001",
"description" : "This is a connector description class1 0001",
"sellDataID" : null,
"duyDataID" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/cadata/66a72af725b3ef6972ae18e9"
},
"connector" : {
"href" : "http://localhost:8080/cadata/66a72af725b3ef6972ae18e9"
}
}
}, {
"cacert" : "cacert ca0001 class2 0002",
"description" : "This is a connector description class1 0002",
"sellDataID" : null,
"duyDataID" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/cadata/66a72af725b3ef6972ae18ea"
},
"connector" : {
"href" : "http://localhost:8080/cadata/66a72af725b3ef6972ae18ea"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/cadata?page=0&size=20"
},
"profile" : {
"href" : "http://localhost:8080/profile/cadata"
},
"search" : {
"href" : "http://localhost:8080/cadata/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 2,
"totalPages" : 1,
"number" : 0
}
}
测试成功~!