小编Yan*_*ski的帖子

Spring数据存储库:列表与流

何时定义方法list以及stream在 Spring 数据存储库中定义什么建议?

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-streaming

例子:

interface UserRepository extends Repository<User, Long> {

  List<User> findAllByLastName(String lastName);

  Stream<User> streamAllByFirstName(String firstName);                    
         
  // Other methods defined.
}
Run Code Online (Sandbox Code Playgroud)

请注意,这里我不是在询问PageSlice - 它们对我来说很清楚,并且我在文档中找到了它们的描述。


我的假设(我错了吗?):

  1. Stream 不会将所有记录加载到 Java 堆中。相反,它将k记录加载到堆中并一一处理它们;然后它加载另一个k记录等等。

  2. List 会立即将所有记录加载到 Java 堆中。

  3. 如果我需要一些后台批处理作业(例如计算分析),我可以使用流操作,因为我不会立即将所有记录加载到堆中。

  4. 如果我需要返回包含所有记录的 REST 响应,我无论如何都需要将它们加载到 RAM 中并将它们序列化为 JSON。在这种情况下,立即加载列表是有意义的。


我看到一些开发人员在返回响应之前将流收集到列表中。

class UserController {

    public ResponseEntity<List<User>> getUsers() {
        return new ResponseEntity(
                repository.streamByFirstName()
                        // OK, for mapper - it is nice syntactic sugar. 
                        // Let's imagine there …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data

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

PMD 插件因 Java 14 而失败:不支持的 targetJdk

我正在尝试将 PMD 插件集成到构建阶段的 pom.xml 文件。

PMD版本 3.13.0

甲骨文 JDK 14

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <pmd.plugin.version>3.13.0</pmd.plugin.version>
    </properties>

    <dependencies>

    </dependencies>


    <reporting>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
            </plugin>

        </plugins>
    </reporting>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${pmd.plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <configuration>
                    <!-- failOnViolation is actually true by default, but can be disabled -->
                    <failOnViolation>true</failOnViolation>
                    <!-- printFailingErrors is pretty useful -->
                    <printFailingErrors>true</printFailingErrors>
                    <targetJdk>14</targetJdk>
                </configuration>
                <executions>
                    <execution>
                        <goals> …
Run Code Online (Sandbox Code Playgroud)

java pmd maven java-14

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

Jackson xml 和 json 根元素

我有一个以 JSON 和 XML 格式返回对象的服务。

http://localhost:8091/apiN/xml/2

XML 结果

<restObjectList>
    <restObjectList>
        <restObjectList>
            <timestamp>2017-06-19 17:01:01</timestamp>
            <title>Rest object</title>
            <fullText>This is the full text. ID: 1</fullText>
            <id>1</id>
            <value>0.1412789210135622</value>
        </restObjectList>
        <restObjectList>
            <timestamp>2017-06-19 17:01:01</timestamp>
            <title>Rest object</title>
            <fullText>This is the full text. ID: 2</fullText>
            <id>2</id>
            <value>0.9886539664938628</value>
        </restObjectList>
    </restObjectList>
</restObjectList>
Run Code Online (Sandbox Code Playgroud)

http://localhost:8091/apiN/2

JSON 结果

{
    "restObjectList": [
        {
            "timestamp": "2017-06-19 17:01:01",
            "title": "Rest object",
            "fullText": "This is the full text. ID: 1",
            "id": 1,
            "value": 0.1412789210135622
        },
        {
            "timestamp": "2017-06-19 17:01:01",
            "title": "Rest object",
            "fullText": "This is the full …
Run Code Online (Sandbox Code Playgroud)

java xml json jackson spring-boot

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

爪哇。是否可以在优先级队列中使用一对,然后使用键作为优先级返回一个值

所以我想使用最小的键作为优先级,然后返回对应键的值:

import javafx.util.Pair;
import java.util.PriorityQueue;

public class Test
{
    public static void main (String[] args)
    {
        int n = 5;

        PriorityQueue <Pair <Integer,Integer> > l = new PriorityQueue <Pair <Integer,Integer> > (n);

        l.add(new Pair <> (1, 90));
        l.add(new Pair <> (7, 54));
        l.add(new Pair <> (2, 99));
        l.add(new Pair <> (4, 88));
        l.add(new Pair <> (9, 89));

        System.out.println(l.poll().getValue()); 
    }
}
Run Code Online (Sandbox Code Playgroud)

我寻找的输出是 90,因为 1 是最小的键。即使将该值用作优先级并返回键也很好,因为我可以在必要时交换数据。我想使用值/键作为优先级(在这种情况下是最小值)来显示键/值。我不知道在这种情况下如何做到这一点。这在 C++ 中工作正常。

java collections priority-queue keyvaluepair

5
推荐指数
2
解决办法
5550
查看次数

在没有 nginx 的情况下运行 Flask 应用程序

在没有 nginx 或其他 Web 服务器的情况下运行 Flask 应用程序?

uWSGI 可以同时作为 Web 服务器和应用程序服务器吗?

例如,独立的 WSGI 容器 https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/ 但同样,它建议使用 HTTP 服务器。为什么?uWSGI 不能处理 HTTP 请求吗?

我阅读了有关部署 Flask 应用程序的不同文章。他们说,我需要 uWSGI 和 nginx——一种流行的选择。

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

https://flask.palletsprojects.com/en/1.1.x/deploying/uwsgi/#uwsgi

我的烧瓶应用程序。app_service.py

import json
import os

from flask import Flask, Response, redirect


portToUse = 9401


@app.route("/app/people")
def get_service_people():
    print("Get people")
    people_str = "{ \"John\", \"Alex\" }"
    return Response(people_str, mimetype="application/json;charset=UTF-8")


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=portToUse)
Run Code Online (Sandbox Code Playgroud)

uwsgi配置uwsgi.ini

[uwsgi]
chdir = $(APPDIR)
wsgi-file = app_service.py
callable = app
uid …
Run Code Online (Sandbox Code Playgroud)

nginx flask uwsgi python-3.x

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

Notepad++,比较插件安装问题

我有 Notepad++ 7.5.8 (npp.7.5.8)。它没有插件管理器;以前的版本曾经有它。我遵循了这些说明。

我是从https://sourceforge.net/projects/npp-compare/下载的

ComparePlugin.readme.txt

安装ComparePlugin.dll到插件目录C:\Program Files\Notepad++\Plugins

我没工作。插件菜单没有比较插件。虽然它有其他插件。

notepad++

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

找不到工件 Jackson-modules-java8:jar

我使用open-jdk-10

mvn clean install
Run Code Online (Sandbox Code Playgroud)

在“中央”中找不到工件 com.fasterxml.jackson.module:jackson-modules-java8:jar:2.9.8

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <jackson.version>2.9.8</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-modules-java8</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

没有jackson-modules-java8它成功构建。

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-modules-java8</artifactId>
    <version>${jackson.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

有一个类似的问题,但它不能解决我的问题。我的 pom 文件中有 maven 依赖项。如果没有其他工作,手动下载东西是我要做的最后一件事。

jackson maven java-10

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

Apache HttpPost 多个文件上传因服务器错误而失败 - 缺少初始多部分边界

我尝试使用 Apache Http 库上传多个文件。

   compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.6'
Run Code Online (Sandbox Code Playgroud)

这就是我上传文件的方式。

String url = "url";
File f1 = new File("file1");
File f2 = new File("file2");
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "multipart/form-data");
request.addHeader("Authorization", "Basic abcd=");
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

FileBody fileBody1 = new FileBody(f1, ContentType.DEFAULT_TEXT);
FileBody fileBody2 = new FileBody(f2, ContentType.DEFAULT_TEXT);

multipartEntityBuilder.addPart("file_1_param", fileBody1);
multipartEntityBuilder.addPart("file_2_param", fileBody2);
HttpEntity httpEntity = multipartEntityBuilder.build();
request.setEntity(httpEntity);
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
if (entity == null) {
    return; …
Run Code Online (Sandbox Code Playgroud)

java file-upload http apache-commons-httpclient

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

Groovy:点击与使用

Groovy 具有tapwith功能。它们之间有什么区别

def user = new User('john', 1)
        .tap {userService.save(it)}

def user2 = new User('Alex', 2)
        .with {userService.save(it)}
Run Code Online (Sandbox Code Playgroud)

userService 更新记录并返回更新的记录。

class UserService {
    public User save(final User user) {
        // save user
        return updated // Id, lastmodified and other fields can be updated.
    }
}
Run Code Online (Sandbox Code Playgroud)

groovy

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