Spring,Java:将通用对象列表作为返回类型传递(Spring, Java : Passing Generic object List as return type)
我正在开发一个Spring-MVC应用程序,根据用户设置的模式,我必须返回一个Object1或Object2的List。 理想情况下,我可以创建两个控制器方法并适当地发送List,但我想知道是否有任何方法,我可以在该Controller方法中发送任何类型的List。
控制器方法:
@PreAuthorize("hasRole('ROLE_USER')") @RequestMapping(value = "/findnotebydays/{days}/{canvasid}/{mode}") public @ResponseBody List<Inotes> findNotesByDays(@PathVariable("days")int days, @PathVariable("canvasid")int canvasid, @PathVariable("mode")boolean mode ){ if(!mode){ return this.groupNotesService.findGroupNotesByDays(days,canvasid); } else { return this.notesService.findNotesByDays(days,canvasid); } }
基本上,如果mode为false,我想返回
List<GroupNotes>
,如果mode为true,我想返回List<Notes>
。 我的天真的方法,我认为我可以说它是一个对象并返回,但似乎不起作用。 请让我知道我能做些什么。 非常感谢。 :-)更新
GroupNotes模型类:
@Entity @Table(name="groupnotes") public class GroupNotes implements Inotes{ @Id @Column(name="mnoteid") @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "mnote_gen") @SequenceGenerator(name = "mnote_gen",sequenceName = "mnote_seq") @org.hibernate.annotations.Index(name = "mnoticesidindex") private int mnoticesid; @Column(name = "mnotetext") private String mnotetext; //Other variables, getters, setters ignored }
Notes模型类:
@Entity @Table(name="note") public class Notes implements Inotes{ @Id @Column(name="noteid") @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "note_gen") @SequenceGenerator(name = "note_gen",sequenceName = "note_seq") @org.hibernate.annotations.Index(name = "noteidindex") private int noticesid; @Column(name = "notetext") private String notetext; //Other variables, getters, setters ignored }
界面信息:
package com.journaldev.spring.model; public interface Inotes { }
I am working on a Spring-MVC application in which depending upon the mode set by the user, I have to return a List of either Object1 or Object2. Ideally, I can create two controller methods and send the List appropriately, but I would like to know is there any way, I can send any type of List in that Controller method.
Controller method :
@PreAuthorize("hasRole('ROLE_USER')") @RequestMapping(value = "/findnotebydays/{days}/{canvasid}/{mode}") public @ResponseBody List<Inotes> findNotesByDays(@PathVariable("days")int days, @PathVariable("canvasid")int canvasid, @PathVariable("mode")boolean mode ){ if(!mode){ return this.groupNotesService.findGroupNotesByDays(days,canvasid); } else { return this.notesService.findNotesByDays(days,canvasid); } }
Basically, if mode is false, I want to return
List<GroupNotes>
and if mode is true, I would like to returnList<Notes>
. My naive approach that I thought I can just say it is an Object and return, but doesn't seem to work. Kindly let me know what I can do. Thanks a lot. :-)Update
GroupNotes model class :
@Entity @Table(name="groupnotes") public class GroupNotes implements Inotes{ @Id @Column(name="mnoteid") @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "mnote_gen") @SequenceGenerator(name = "mnote_gen",sequenceName = "mnote_seq") @org.hibernate.annotations.Index(name = "mnoticesidindex") private int mnoticesid; @Column(name = "mnotetext") private String mnotetext; //Other variables, getters, setters ignored }
Notes model class :
@Entity @Table(name="note") public class Notes implements Inotes{ @Id @Column(name="noteid") @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "note_gen") @SequenceGenerator(name = "note_gen",sequenceName = "note_seq") @org.hibernate.annotations.Index(name = "noteidindex") private int noticesid; @Column(name = "notetext") private String notetext; //Other variables, getters, setters ignored }
Interface Inotes :
package com.journaldev.spring.model; public interface Inotes { }
原文:https://stackoverflow.com/questions/30868002
满意答案
这是一个优雅,Pythonic的做法:
>>> array([[1,2,3],]*3) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) >>> array([[1,2,3],]*3).transpose() array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
[16]
的问题似乎是转置对数组没有影响。 你可能想要一个矩阵:>>> x = array([1,2,3]) >>> x array([1, 2, 3]) >>> x.transpose() array([1, 2, 3]) >>> matrix([1,2,3]) matrix([[1, 2, 3]]) >>> matrix([1,2,3]).transpose() matrix([[1], [2], [3]])
Here's an elegant, Pythonic way to do it:
>>> array([[1,2,3],]*3) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) >>> array([[1,2,3],]*3).transpose() array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
the problem with
[16]
seems to be that the transpose has no effect for an array. you're probably wanting a matrix instead:>>> x = array([1,2,3]) >>> x array([1, 2, 3]) >>> x.transpose() array([1, 2, 3]) >>> matrix([1,2,3]) matrix([[1, 2, 3]]) >>> matrix([1,2,3]).transpose() matrix([[1], [2], [3]])
相关问答
更多使用存储在向量中的行/列指示填充矩阵的最快方法(Fastest way to populate a matrix using row/column indicies stored in vectors)
如何将行向量转换为Eigen中的列向量?(How to convert row vector to column vector in Eigen?)
将第一行(列标题)写入向量(writing first row (column headers) to a vector)
“克隆”行或列向量(“Cloning” row or column vectors)
获取向量中矩阵索引的列和行名称(Get column and row names of matrix indices in a vector)
使用几个大向量过滤行(Filter rows with several large vectors)
MATLAB使用行和列索引向量访问稀疏矩阵中的多个元素(MATLAB accessing multiple elements in sparse matrix using row and column index vectors)
将一个热行向量的numpy数组转换为索引的列向量(Turn a numpy array of one hot row vectors into a column vector of indices)
使用不同长度的字符向量构建数据框列?(Build column of data frame with character vectors of different length?)
根据行和列索引向量分配新的矩阵值(assign new matrix values based on row and column index vectors)
相关文章
更多Object Oriented Programming
redis整合spring示例二—java操作redis(存对象及List)
Solr Cache使用介绍及分析,包括LRUCache、filterCache、queryResultCache、documentCache、Generic Caches
java List排序一
java通用返回对象
Python 列表(list)操作
Guava学习笔记:复写的Object常用方法
hibernate list查询 报类型转换异常
ServletOutputStream cannot be resolved to a type
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!