小编Aja*_*gar的帖子

通过服务公开JPA实体

我们正在使用AngularJS,Rest,JPA开发Web应用程序.我已经阅读了一些关于域实体不应该通过服务公开的文章.我知道这是紧密耦合,可能存在循环引用,关注点分离,这似乎对我有用.但后来我看到有关将jpa和jaxb映射应用于同一模型的文章,eclipseLink moxy就是一个例子.

然后是Spring数据REST,通过rest api公开jpa实体.(可能是Spring Data REST用来解决手头的不同问题)

所以我有点困惑.回答以下两个问题和场景,其中一个更好,另一个会有所帮助.

  1. 将jaxb和JPA注释应用于同一域模型有什么好处?这样做主要是为了避免层间的DTO吗?

  2. 是否应该仅在开发需要公开CRUD操作的应用程序时才使用Spring数据REST,并且实际上没有涉及其他业务功能?

java spring-data

6
推荐指数
1
解决办法
1590
查看次数

使用 Java 8 的装饰器模式

维基百科这里有一个装饰器模式的例子:

https://en.wikipedia.org/wiki/Decorator_pattern#Second_example_.28coffee_making_scenario.29

我试图使用 Java 8 使用函数式风格来解决这个问题,我提出的解决方案是:

1.CoffeeDecorator.java

public class CoffeeDecorator {

public static Coffee getCoffee(Coffee basicCoffee, Function<Coffee, Coffee>... coffeeIngredients) {

    Function<Coffee, Coffee> chainOfFunctions = Stream.of(coffeeIngredients)
                                                      .reduce(Function.identity(),Function::andThen);
    return chainOfFunctions.apply(basicCoffee);
}

public static void main(String args[]) {

    Coffee simpleCoffee = new SimpleCoffee();
    printInfo(simpleCoffee);

    Coffee coffeeWithMilk = CoffeeDecorator.getCoffee(simpleCoffee, CoffeeIngredientCalculator::withMilk);
    printInfo(coffeeWithMilk);

    Coffee coffeeWithWSprinkle = CoffeeDecorator.getCoffee(coffeeWithMilk,CoffeeIngredientCalculator::withSprinkles);       
    printInfo(coffeeWithWSprinkle);

}

public static void printInfo(Coffee c) {
    System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
}
Run Code Online (Sandbox Code Playgroud)

}

2.CoffeeIngredientCalculator.java

public class CoffeeIngredientCalculator {

public static Coffee withMilk(Coffee …
Run Code Online (Sandbox Code Playgroud)

design-patterns functional-programming java-8

5
推荐指数
1
解决办法
2771
查看次数