首页 \ 问答 \ Spring,Java:将通用对象列表作为返回类型传递(Spring, Java : Passing Generic object List as return type)

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 return List<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
更新时间:2023-03-14 10:03

最满意答案

这是一个优雅,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]])

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)