小编jan*_*nis的帖子

Spring Data:多个存储库接口到单个“存储库”服务类中

JpaRepository由于数据库的设计,我有相当多的扩展 Repository 接口。

为了构造一个简单的对象,即Person我必须对大约 4 - 5 个存储库进行方法调用,因为数据像这样分布在整个数据库中。像这样(原谅伪代码):

@Service
public class PersonConstructService {

    public PersonConstructService(Repository repository,
                                  RepositoryTwo repositoryTwo,
                                  RepositoryThree repositoryThree) {

        public Person constructPerson() {
            person
                .add(GetDataFromRepositoryOne())
                .add(GetDataFromRepositoryTwo())
                .add(GetDataFromRepositoryThree());

            return person;
        }

        private SomeDataTypeReturnedOne GetDataFromRepositoryOne() {
            repository.doSomething();
        }

        private SomeDataTypeReturnedTwo GetDataFromRepositoryTwo() {
            repositoryTwo.doSomething();
        }

        private SomeDataTypeReturnedThree GetDataFromRepositoryThree() {
            repositoryThree.doSomething();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PersonConstructServiceclass 使用所有这些接口只是为了构造一个简单的Person对象。我从PersonConstructService类中的不同方法调用这些存储库。我曾考虑将这个类扩展到多个类中,但我认为这是不正确的。

相反,我想使用一个repositoryService包含创建Person对象所需的所有存储库。这是一个好方法吗?春天可以吗?

我问的原因是有时注入一个类的服务数量约为 7-8。这绝对不好。

java spring spring-data spring-data-jpa

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

Junit无法使用Spark Structured Streaming创建的文件删除@TempDir

我为管道创建了集成测试,以检查是否生成了正确的CSV文件:

class CsvBatchSinkTest {

    @RegisterExtension
    static SparkExtension spark = new SparkExtension();

    @TempDir
    static Path directory;

    //this checks if the file is already available
    static boolean isFileWithSuffixAvailable(File directory, String suffix) throws IOException {
        return Files.walk(directory.toPath()).anyMatch(f -> f.toString().endsWith(suffix));
    }

    //this gets content of file
    static List<String> extractFileWithSuffixContent(File file, String suffix) throws IOException {
        return Files.readAllLines(
                Files.walk(file.toPath())
                        .filter(f -> f.toString().endsWith(suffix))
                        .findFirst()
                        .orElseThrow(AssertionException::new));
    }

    @Test
    @DisplayName("When correct dataset is sent to sink, then correct csv file should be generated.")
    void testWrite() throws IOException, InterruptedException …
Run Code Online (Sandbox Code Playgroud)

java junit tempdir temporary-directory

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

Mapstruct Java11 兼容吗?

我有点困惑。有一些文档说 java 9 是“实验性的”:

https://mapstruct.org/documentation/stable/reference/html/#_using_mapstruct_on_java_9

我发现了一个帖子,其中有人在 Java 10 中遇到了问题。所以我们将前往 Java 11,我想知道 Mapstuct 是否可以在该环境中工作。具体来说,它会在编译时生成代码并且生成的代码在那里工作(我想后者会)。

mapstruct java-11

4
推荐指数
2
解决办法
7073
查看次数

无法读取 HTTP 消息:org.s.HttReadableException:无法读取文档:无法识别的令牌“PUT”:正在等待(“true”、“false”或“null”)

我正在 Spring STS 中实现 SPRING 数据 JPA+ Oracle...它是邮递员中的示例应用程序,我收到 Get 方法的响应 200,但对于 post 和 put 无法从数据库读取数据,我猜并给出以下错误 -

无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法识别的令牌“PUT”:在 [来源:java.io. PushbackInputStream@1f1076e7; 行:1,列:5];嵌套异常是 com.fasterxml.jackson.core.JsonParseException:无法识别的标记“PUT”:在 [来源:java.io.PushbackInputStream@1f1076e7; 处期待(“true”、“false”或“null”);行:1,列:5]

@Service
public class PersonService {
    @Autowired
    private PersonRepository personRepository;


    public Object findAll() {
        return personRepository.findAll();
    }

    public Person findById(Long id) {
        return personRepository.findOne(id);
    }

    public Person save(Person person) {
        return personRepository.save(person);
    }


    public Person delete(Person person) {
        personRepository.delete(person);
        return person;
    }

    public Person findByEmail(String email){ return null; }
}
Run Code Online (Sandbox Code Playgroud)

控制器方法:

@RequestMapping(value = "/all", method = …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-data-jpa

3
推荐指数
1
解决办法
2953
查看次数

如何设计REST API,需要资源创建和修改才能被另一方批准

我必须为需要双重控制的安全敏感资源设计REST服务.设计实现双重控制的REST服务的优雅方法是什么?

双重控制

对于双重控制,我的意思是,如果多人(例如2人)参与变更,则变更才会生效.

例如,我有一个名为userProfile的资源.它定义了允许用户执行的所有操作.如果有人想要更改此类个人资料,则可以建议对其进行更改.然后必须验证此更改.此验证的结果是"批准"或"拒绝".一旦变更获得批准,它就会生效.

设计

我目前有一个userProfile资源和一个userProfileChangeProposal资源.

创建一个创建userprofile的提议

POST /userprofiles
Run Code Online (Sandbox Code Playgroud)

这将返回userprofile的ID.现在可以使用{id}进行验证

PUT /userprofiles/{id}/changeproposal
Run Code Online (Sandbox Code Playgroud)

删除或更新userprofile需要再次提交,因此:

DELETE /userprofiles/{id}
PUT /userprofiles/{id}
Run Code Online (Sandbox Code Playgroud)

可以通过以下方式再次验证这些更改:(对于用户配置文件,同时只能有1个提案)

PUT /userprofiles/{id}/changeproposal
Run Code Online (Sandbox Code Playgroud)

问题

我正在努力的事情是,其余的操作似乎在userprofile资源上运行,但事实上他们不这样做.删除不会直接删除资源.它会创建一个删除它的提议.此外,此方法不允许直接删除用户配置文件.

另一方面,如果通过更改提议进行所有更改,则所有创建/删除/更新操作都是正确的

CREATE /userprofilechangeproposal
Run Code Online (Sandbox Code Playgroud)

我没有在互联网上看到有关双控制设计的任何内容.最接近的是有人首先创建ORDER资源,并且只有在订单被批准后才会创建实际的CAR.

任何人都有最佳做法?

rest

2
推荐指数
1
解决办法
587
查看次数

java.time.DateTimeException:无法从时间中提取 ZoneId

当我运行第一段时,它非常好并生成一个输出。但是在第二种情况下,当我运行此段 2 时,它会生成

DateTimeException : Unable to extract ZoneId from temporal.
Run Code Online (Sandbox Code Playgroud)

第 1 部分:

LocalDate ld = LocalDate.now();
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ld));
Run Code Online (Sandbox Code Playgroud)

第 2 部分:

LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(ldt));
Run Code Online (Sandbox Code Playgroud)

java datetime-format java-time

2
推荐指数
1
解决办法
2197
查看次数

Java文件列表与Window资源管理器相同的顺序

我正在使用下面的代码来获取文件列表排序:(如窗口资源管理器)

    package com.codnix.quickpdfgenerator.testing;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class FileListOrder {
        public static void main(String args[]) {
            //huge test data set ;)

            File folder = new File("C:\\Users\\Codnix\\Desktop\\Test Sequence");
            File[] listOfFiles = folder.listFiles();
            List<File> filenames = Arrays.asList(listOfFiles); 

            //adaptor for comparing files
            Collections.sort(filenames, new Comparator<File>() {
                private final Comparator<String> NATURAL_SORT = new WindowsExplorerComparator();

                @Override
                public int compare(File o1, File o2) {;
                    return NATURAL_SORT.compare(o1.getName(), o2.getName());
                }
            });

            for (File …
Run Code Online (Sandbox Code Playgroud)

java explorer natural-sort

2
推荐指数
1
解决办法
317
查看次数

宁静的Web参数篡改

我试图了解如何使用restful客户端为经过身份验证的用户实现安全性.我遇到麻烦的情况是如何阻止用户更新不属于他自己的购买,因为宁静的客户通过了购买ID.对于熟练的用户,产品id可以容易地被篡改.由于使用https可以阻止或减轻它,因此我并不感兴趣.我真的对试图更新不属于他们的用户感兴趣.

你如何在其他世界阻止这种攻击?Web参数篡改

security rest authorization web-applications rest-security

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

为什么当有一个尚未完成的完成阶段时主线程不终止?

这是我的简单代码:

public class Main4 {
    public static void main(String[] args) {
        System.out.println("Hello from thread: "+Thread.currentThread().getName());
        new Game().run();
        System.out.println("I am dying ... ");
    }

    static class Game {
        public void run() {
            value();
        }

        private int value() {
            int number = 0;
            CompletionStage<Void> c = calculate().thenApply(i -> i + 3).thenAccept(i -> System.out.println("I am done, and my value is " + i));
            return number;
        }

        private CompletionStage<Integer> calculate() {
            CompletionStage<Integer> completionStage = new CompletableFuture<>();
            Executors.newCachedThreadPool().submit(() -> {
                System.out.println("I am in the thread: …
Run Code Online (Sandbox Code Playgroud)

java multithreading threadpool completable-future completion-stage

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

为什么 Groovy eachDir() 每次都给我相同的排序?

我正在创建一个包含子目录列表的文件

task createNotes {
  doLast {
    def myFile = new File("my-notes.txt")
    def file = new File("src/test/")
    println myFile.exists()
    myFile.delete()
    println myFile.exists()
    println myFile.absolutePath
    println file.absolutePath
    myFile.withWriter {
      out ->
        file.eachDir { dir ->
          out.println dir.getName()
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

显然不能保证排序顺序,但每次运行它时,我都会得到相同的排序顺序:

soft
java
calc
conc
caab
pres
Run Code Online (Sandbox Code Playgroud)

如果我将“soft”目录更改为“sofp”,则输出为:

java
sofp
calc
conc
caab
pres
Run Code Online (Sandbox Code Playgroud)

当我把名字改回来时,它会回到原来的顺序。

它似乎没有按任何特定顺序排序 - 这与说明无法保证顺序的文档相匹配,但如果是这样,为什么每次总是给我相同的排序?

java groovy jvm gradle

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