我有以下Projection返回标签名称:
@Projection(types = NewsTag.class, name = "newsTag")
public interface NewsTagProjection {
@Value("#{target.tag.name}")
String getName();
}
我在负责newsTag模型的以下存储库中使用它:
@RepositoryRestResource(excerptProjection = NewsTagProjection.class)
public interface NewsTagRepository extends JpaRepository<NewsTag, Integer> {
}
因此,当我调用具有NewsTag列表的新闻存储库时:
@RepositoryRestResource
public interface NewsRepository extends JpaRepository<News, Integer> {
}
一切正常,它只返回nae标签列表。
但是,如果我想使用具有相同端点的自定义控制器(可以说我正在为错误处理做这件事),它只会向我返回没有标签列表的新闻,例如不存在投影。
这是控制器:
@RepositoryRestController
@RequestMapping("news")
public class NewsController {
private NewsService newsService;
private PagedResourcesAssembler<News> pagedAssembler;
private NewsResourceAssembler newsResourceAssembler;
@Autowired
public void setNewsService(NewsService newsService) {
this.newsService = newsService;
}
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
public void setPagedAssembler(PagedResourcesAssembler<News> pagedAssembler) {
this.pagedAssembler = pagedAssembler;
}
@Autowired
public void setNewsResourceAssembler(NewsResourceAssembler newsResourceAssembler) {
this.newsResourceAssembler = newsResourceAssembler;
}
// Return all news with pagination
@GetMapping
public ResponseEntity<?> getAllNews(Pageable pageable) {
Page<News> newsPage = this.newsService.getAllNews(pageable);
return ResponseEntity.ok(this.pagedAssembler.toResource(newsPage, this.newsResourceAssembler));
}
}
编辑
NewsResourceAssembler:
@Service
public class NewsResourceAssembler implements ResourceAssembler<News, Resource<News>> {
private EntityLinks entityLinks;
@Autowired
public void setEntityLinks(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<News> toResource(News news) {
Link self = entityLinks.linkFor(News.class).slash(news.getId()).withSelfRel();
Link newsTags = entityLinks.linkFor(News.class).slash(news.getId()).slash("newsTags").withRel("newsTags");
Link newsComments = entityLinks.linkFor(News.class).slash(news.getId()).slash("newsComments").withRel("newsComments");
return new Resource<>(news, self, newsTags, newsComments);
}
}
最佳答案
如果要添加新闻的所有字段,则可以操作ResponseEntity的返回对象。所以我更改了此代码,您可以申请代码吗?NewsResourceAssembler.class
应该像;
@Service
public class NewsResourceAssembler implements ResourceAssembler<News, Resource<NewsResource>> {
private EntityLinks entityLinks;
@Autowired
public void setEntityLinks(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<NewsResource> toResource(News news) {
Link self = entityLinks.linkFor(News.class).slash(news.getId()).withSelfRel();
Link newsTags = entityLinks.linkFor(News.class).slash(news.getId()).slash("newsTags").withRel("newsTags");
Link newsComments = entityLinks.linkFor(News.class).slash(news.getId()).slash("newsComments").withRel("newsComments");
final NewsResource newsResource = new NewsResource();
//set any fields to which do you wants to send client
//newsResource.setExampleFields();
//newsResource.setNewsTagList(news.getNewsTag()); //example code. Change according to your models
return new Resource(newsResource, self, newsTags, newsComments);
}
}
现在您需要
NewsResource.class
,所以应该像这样;@JsonIgnoreProperties(ignoreUnknown = true)
public class NewsResource extends ResourceSupport {
//private fields of which do you wants
//dont forget list of newstag field
//ex : private List<NewsTagFields> newsTag = new ArrayList<>();
//Also should write getter and setter methods
}
PS:别忘了注释行的规则。根据此更改,您的客户应分析对客户的传入响应。因为作为响应,某些链接rel应该在主对象上更改。但是您的自定义参数没有改变。
编辑:
如果在
NewsResource.class
的newsTag
->字段中应该是这样(请不要忘记在资源汇编程序类中设置所有标签!);private List<NewsTagFields> newsTag = new ArrayList<>();
现在,您需要NewsTagFields.class。你也可以使用这个类;
public class NewsTagFields {
private final String id;
private final String name;
public NewsTagFields(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
最后,将此代码添加到
ResourceAssembler
中以添加新的NewsTagFields列表;(此代码提供了获取所有newsTag并将其完全转换为NewsTagFields列表的功能。并添加到newsResource以发送给客户端)
final List<NewsTagFields> allNewsTag =
news.getNewsTag().stream()
.map(newsTag -> new NewsTagFields(newsTag.getId(), newsTag.getName()))
.collect(Collectors.toList());
newsResource.setNewsTagList(allNewsTag);