spring-roo - 从 REST 服务填充的 Spring Roo 模型(无持久性)

标签 spring-roo

我正在构建一个没有本地持久性的小型 Web 应用程序。它从远程 RESTful 端点加载数据。

它有一个 Model 对象,称为 Order。当 web 应用程序收到诸如 example.com/order/1234 之类的请求时,我希望 web 应用程序向远程 REST 端点发出 GET 请求(来自服务层?),然后填充我的域模型。

解决这个问题的最佳方法是什么?
在没有设置持久层的情况下,以任何有用的方式使用 roo 是否可能且值得?

最佳答案

2个选项:

  • 仅在内存中使用 H2 持久性提供程序
  • 正常创建 roo 实体,然后推送所有方法并覆盖以使用本地 HashMap。我以前这样做过,如下所示:
    @RooJavaBean
    @RooToString
    @RooJpaActiveRecord(versionField="", identifierField="name", identifierType=String.class, versionType=Byte.class)
    @RooJson
    public class MonitorMetric {
    static Map<String, MonitorMetric> data=new HashMap<String, MonitorMetric>();
    
    @Id
    private String name;
    
    long totTime;
    long minTime=Long.MAX_VALUE;
    long maxTime=Long.MIN_VALUE;
    int countStarted;
    int countFinished;
    
    @DateTimeFormat(style = "SS")
    private Date lastAttempt;
    @DateTimeFormat(style = "SS")
    private Date lastSuccess;
    @DateTimeFormat(style = "SS")
    private Date lastFailure;
    
    public double getAvgTime() {
        return (countFinished>0 ? totTime/countFinished : 0);
    }
    
    public String getName() {
        return this.name;
    }
    
    public void setName(String id) {
        this.name = id;
    }
    
    public static long countMonitorMetrics() {
        return data.size();
    }
    
    public static List<MonitorMetric> findAllMonitorMetrics() {
        return new ArrayList<MonitorMetric>(data.values());
    }
    
    public static List<MonitorMetric> findMonitorMetricEntries(int firstResult, int maxResults) {
        int lastResult = (firstResult+maxResults > data.size() ? data.size() : firstResult+maxResults);
        return new ArrayList<MonitorMetric>(data.values()).subList(firstResult, lastResult);
    }
    
    public static MonitorMetric findMonitorMetric(String name) {
        if (name == null || name.length() == 0) return null;
        return data.get(name);
    }
    
    public void persist() {
        data.put(getName(), this);
    }
    
    public MonitorMetric merge() {
        data.put(getName(), this);
        return this;
    }
    
    public void remove() {
        data.remove(getName());
    }
    
    public void flush() {
    }
    
    public void clear() {
        data.clear();
    }
    }
    
  • 关于spring-roo - 从 REST 服务填充的 Spring Roo 模型(无持久性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12082333/

    相关文章:

    hibernate - 在双向多对一关系中从非所有者方更新实体

    java - Hibernate @OneToOne child 不坚持

    java - 如何在spring roo项目中调用.aj文件中的方法?

    java - spring事务注释方法无需对实体进行合并调用

    gwt - Spring MVC Maven 目标不适用于 GWT

    java - Spring Roo 演示和模型绑定(bind)

    maven-2 - 在嵌入式 jetty 热部署

    java - Spring roo - jetty 无法启动

    java - Solr + Spring Roo 为每个客户提供单独的索引

    Eclipse、JPA 2.0 元模型生成器和 Spring Roo 协同工作