@RestController
@Transactional
public class ApiController {
@Autowired
CategoryService categoryService;
@SuppressWarnings("deprecation")
@RequestMapping(value="/api/category/page", method=RequestMethod.GET)
public Page<Category> getCategoryList() {
return categoryService.findAll(new PageRequest(0, 10));
}
}
我想检索要在angularjs中使用的Page,该页面什么也不返回:{}
。我getContent ()
仍可应要求提供数据:
[{"cid": 1, "cname": "Tien Hiep", "cmetaTitle": "tien-hiep",
"createDate": "May 6, 2018 1:04:58 PM "," createBy ":" Admin ","
modifiedDate ":" May 28, 2018 11:09:57 AM "," modifiedBy ":" Admin
"," cstatus " ..]
如果我想返回如下页面类型:
{
"content":[
{"cid": 1, "cname": "Tien Hiep", "cmetaTitle": "tien-hiep", "createDate": "May 6, 018 1:04:58 PM "," createBy ":" Admin "," modifiedDate ":" May 28, 2018 11:09:57 AM "," modifiedBy ":" Admin "," cstatus "},
...
],
"last":false,
"totalElements":10,
"totalPages":4,
"size":10,
"number":0,
"sort":null,
"first":true,
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
这是因为您要在此处返回List,如果希望响应的格式应为所需的格式,则应包括DTO或将该响应绑定到类中并返回该类。
下面我给出了一些小代码:
FoDto类{
列出内容; / Getter and Setter方法/
}
在服务层,您可以准备响应。
答案 1 :(得分:0)
如果您正在使用maven
1。添加以下jersey依赖项以将Java对象列表转换为json
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
或者您可以添加Jax-rs依赖项
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
2)将方法返回类型更改为
List<Category>
代替Page<Category>
3)在上述API中添加@Produces("application/json")
注释。这是按照您在问题中提到的将响应转换为json格式。
您的控制器方法的最终结果。
@RequestMapping(value="/api/category/page", method=RequestMethod.GET)
@Produces("application/json")
public List<Category> getCategoryList() {
Page<Category> pageCategory = categoryService.findAll(new PageRequest(0, 10));
List<Category> catgoryList = pageCategory.getContent(); /*missed this conversion in my original post */
return catgoryList;
}
如果您有任何问题或疑问,请告诉我。
答案 2 :(得分:0)
假设您的Dao像下面的代码:
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
public interface CategoryDao extends CrudRepository<Category,Integer>{
List<Category> findAll(Pageable pageable);
}
您的分页实用程序类是这样的:
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
public class PaginationUtil {
public static PageRequest makePageRequest(int pageId, int pageSize, Sort.Direction direction, String... properties){
return pageSize> 0?new PageRequest(pageId, pageSize, direction, properties):null;
}
}
现在,您可以通过波纹管方式获取分页数据:
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
@Autowired
private CategoryDao dao;
PageRequest pageRequest = PaginationUtil.makePageRequest(0, 10, Sort.Direction.DESC, "id");
List<Category> categories = dao
.findAll(pageRequest);
现在类别列表中包含了所有数据。
您的终结点代码如下:
@RequestMapping(value="/api/category/page", method=RequestMethod.GET)
@Produces("application/json")
public List<Category> getCategoryList() {
PageRequest pageRequest = PaginationUtil.makePageRequest(0, 10, Sort.Direction.DESC, "id");
List<Category> categories = dao
.findAll(pageRequest);
return categories;
}
更多信息,请访问this thread。
谢谢:)