beg*_*er_ 25 java json spring-mvc spring-data spring-hateoas
我有一个用Spring-Data制作的数据访问层.我现在正在创建一个Web应用程序.这个控制器方法应返回格式为JSON 的Spring-Data页面.
这样的页面是一个列表,其中包含额外的分页信息,例如记录总数等等.
这是可能的,如果是的话怎么样?
与此直接相关,我可以定义属性名称的映射吗?例如.意思是我需要定义如何在JSON中命名分页信息属性(与页面不同).这可能吗?怎么样?
Oli*_*ohm 55
支持Spring HATEOAS和Spring Data Commons即将发布的场景.Spring HATEOAS附带的PageMetadata
对象基本上包含与a 相同的数据,Page
但执行方式较少,因此可以更容易地编组和解组.
我们与Spring HATEOAS和Spring Data共同实现这一点的原因的另一个方面是,简单封送页面,内容和元数据几乎没有价值,但也想生成可能存在的下一页或上一页的链接,以便客户端不必构造URI来遍历这些页面本身.
假设一个域类Person
:
class Person {
Long id;
String firstname, lastname;
}
Run Code Online (Sandbox Code Playgroud)
以及它的相应存储库:
interface PersonRepository extends PagingAndSortingRepository<Person, Long> { }
Run Code Online (Sandbox Code Playgroud)
您现在可以按如下方式公开Spring MVC控制器:
@Controller
class PersonController {
@Autowired PersonRepository repository;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
HttpEntity<PagedResources<Person>> persons(Pageable pageable,
PagedResourcesAssembler assembler) {
Page<Person> persons = repository.findAll(pageable);
return new ResponseEntity<>(assembler.toResources(persons), HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
这里可能有相当多的解释.让我们一步一步来:
@Enable(Jpa|Mongo|Neo4j|Gemfire)Repositories
或等效的XML).控制器方法映射到/persons
,这意味着它将接受GET
对该方法的所有请求.PagedResources
- 来自Spring HATEOAS的一种类型,它表示一些富含Links
加号a的内容PageMetadata
.调用该方法时,Spring MVC必须为Pageable
和创建实例PagedResourcesAssembler
.为了实现这一点,您需要通过@EnableSpringDataWebSupport
即将在Spring Data Commons的里程碑中引入的注释或通过独立的bean定义(此处记录)来启用Spring Data Web支持.
该Pageable
会从请求信息来填充.默认配置将转向?page=0&size=10
成Pageable
由10个页面大小请求的第一页.
将PageableResourcesAssembler
让你轻松转Page
成一个PagedResources
实例.它不仅会将页面元数据添加到响应中,还会根据您访问的页面以及如何Pageable
配置分辨率,将相应的链接添加到表示中.
为JPA启用此示例的示例JavaConfig配置如下所示:
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableJpaRepositories
class ApplicationConfig {
// declare infrastructure components like EntityManagerFactory etc. here
}
Run Code Online (Sandbox Code Playgroud)
假设我们Persons
在数据库中有30个.您现在可以触发请求GET http://localhost:8080/persons
,您将看到与此类似的内容:
{ "links" : [
{ "rel" : "next", "href" : "http://localhost:8080/persons?page=1&size=20 }
],
"content" : [
… // 20 Person instances rendered here
],
"pageMetadata" : {
"size" : 20,
"totalElements" : 30,
"totalPages" : 2,
"number" : 0
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,汇编程序生成了正确的URI,并且还选择了当前的默认配置,以将参数解析Pageable
为即将到来的请求.这意味着,如果更改该配置,链接将自动遵循更改.默认情况下,汇编程序指向它所调用的控制器方法,但可以通过交换自定义Link
作为基础来定制,以构建PagedResourcesAssembler.toResource(…)
方法重载的分页链接.
这些PagedResourcesAssembler
位将在即将发布的Spring Data Babbage 版本列的里程碑版本中提供.它已在当前快照中可用.您可以在我的Spring RESTBucks 示例应用程序中看到这个实例.简单地克隆它,运行mvn jetty:run
和卷曲http://localhost:8080/pages
.
奥利弗,你的答案很棒,我把它标记为答案.这里只是为了完整性我想出的平均时间可能对其他人有用.
我使用JQuery Datatables作为我的网格/表格小部件.它向服务器发送非常具体的参数,并且除了非常具体的响应之外:请参阅http://datatables.net/usage/server-side.
为了实现这一点,创建了一个反映数据表所需内容的自定义辅助对象.请注意,getter和setter必须像它们一样命名,否则生成的json是错误的(区分大小写的属性名称和数据表使用此"伪匈牙利表示法"...).
public class JQueryDatatablesPage<T> implements java.io.Serializable {
private final int iTotalRecords;
private final int iTotalDisplayRecords;
private final String sEcho;
private final List<T> aaData;
public JQueryDatatablesPage(final List<T> pageContent,
final int iTotalRecords,
final int iTotalDisplayRecords,
final String sEcho){
this.aaData = pageContent;
this.iTotalRecords = iTotalRecords;
this.iTotalDisplayRecords = iTotalDisplayRecords;
this.sEcho = sEcho;
}
public int getiTotalRecords(){
return this.iTotalRecords;
}
public int getiTotalDisplayRecords(){
return this.iTotalDisplayRecords;
}
public String getsEcho(){
return this.sEcho;
}
public List<T> getaaData(){
return this.aaData;
}
}
Run Code Online (Sandbox Code Playgroud)
第二部分是相应控制器中的方法:
@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody String search (
@RequestParam int iDisplayStart,
@RequestParam int iDisplayLength,
@RequestParam int sEcho, // for datatables draw count
@RequestParam String search) throws IOException {
int pageNumber = (iDisplayStart + 1) / iDisplayLength;
PageRequest pageable = new PageRequest(pageNumber, iDisplayLength);
Page<SimpleCompound> page = compoundService.myCustomSearchMethod(search, pageable);
int iTotalRecords = (int) (int) page.getTotalElements();
int iTotalDisplayRecords = page.getTotalPages() * iDisplayLength;
JQueryDatatablesPage<SimpleCompound> dtPage = new JQueryDatatablesPage<>(
page.getContent(), iTotalRecords, iTotalDisplayRecords,
Integer.toString(sEcho));
String result = toJson(dtPage);
return result;
}
private String toJson(JQueryDatatablesPage<?> dt) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate4Module());
return mapper.writeValueAsString(dt);
}
Run Code Online (Sandbox Code Playgroud)
compoundService
由Spring-Data存储库支持.它管理事务和方法级别的安全性.toJSON()
方法使用Jackson 2.0,你需要将相应的模块注册到映射器,在我的情况下为hibernate 4.
如果您有双向关系,则需要使用注释来注释所有实体类
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="jsonId")
Run Code Online (Sandbox Code Playgroud)
这使Jackson 2.0能够序列化循环依赖(在早期版本中是不可能的,并且需要对您的实体进行注释).
您需要添加以下依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.1</version>
<type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24250 次 |
最近记录: |