Java 避免加入类spring boot的不必要字段
我不熟悉spring boot及其实现 问题来了:我必须Java 避免加入类spring boot的不必要字段,java,spring,spring-boot,postgresql-9.1,Java,Spring,Spring Boot,Postgresql 9.1,我不熟悉spring boot及其实现 问题来了:我必须模型课歌曲、电影 电影ID属于歌曲类,我和电影一起加入了这个表格 在控制器中,如何实现排除电影中除电影ID和标题以外的所有字段 我的控制器类是 @RestController @RequestMapping("api") public class SongsController { @Autowired SongsRepository songsRepository; @RequestMapping(value = "/Song
模型课歌曲、电影
电影ID属于歌曲类,我和电影一起加入了这个表格
在控制器中,如何实现排除电影
中除电影ID
和标题
以外的所有字段
我的控制器类是
@RestController
@RequestMapping("api")
public class SongsController {
@Autowired
SongsRepository songsRepository;
@RequestMapping(value = "/Songs", method = RequestMethod.GET)
public ResponseEntity<List<Songs>> getSongsList(){
return new ResponseEntity<>(songsRepository.findAll(), HttpStatus.OK);}
songs class has (id , movieid, songtitile.....)
movie class has (movieid, moviename, ..........................)
@RestController
@请求映射(“api”)
公共类歌曲控制器{
@自动连线
歌曲还原歌曲还原;
@RequestMapping(value=“/Songs”,method=RequestMethod.GET)
公共响应getSongsList(){
返回新的ResponseEntity(songsRepository.findAll(),HttpStatus.OK);}
歌曲类有(id、电影id、歌曲……)
电影类有(电影ID、电影名称等)
有人实现了这个库吗
@实体
公开课歌曲{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私人弦乐歌曲名称;
@许多酮
@JoinColumn(name=“movieId”)
私人电影资讯电影资讯;
私人国际年;
私人名单歌手;
私人名单歌词;
私人浮动长度;
....}}
@实体
公共级电影{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长影片;
私有字符串标题;
私人弦年;
私有字符串控制器;
私人弦乐体裁;
...}}
您可能应该使用弹簧投影。它的设计正是为了满足你的要求
只要看一看假设如果您可以共享
歌曲
和电影
pojo以查看关系可能会有所帮助。假设您想排除其他参数,如果我提到@JsonIgnore将影响我的电影服务,您可以包括@JsonIgnore
json视图中所述的@JsonIgnore
。我尝试在songs pojo中提到忽略注释,并在json视图中提到包含函数。我没有得到预期的输出。我尝试了投影,但在这种情况下,我无法生成预期的json。为什么不呢?你能提供更多关于你尝试的内容和你想要的结构的细节吗?在我的歌曲实体中,我只想显示电影实体的三个字段movieid,name,title。我是否需要写单独的投影类项目,再次提及所有字段。是的。定义一个接口(而不是类),只为您需要的字段提供getter。然后,您可以在存储库类中使用它。在纪录片中,我在电影课和歌曲课上都有一年的时间。。如何在接口中编写该代码
@Entity
public class Songs {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String songTitle;
@ManyToOne
@JoinColumn(name = "movieId")
private MoviesInfo moviesInfo;
private int year;
private List<String> singers;
private List<String> lyricists;
private float length;
....}}
@Entity
public class Movies {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long movieId;
private String title;
private String year;
private String director;
private String genre;
...}}