何时定义方法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)
请注意,这里我不是在询问Page、Slice - 它们对我来说很清楚,并且我在文档中找到了它们的描述。
我的假设(我错了吗?):
Stream 不会将所有记录加载到 Java 堆中。相反,它将k记录加载到堆中并一一处理它们;然后它加载另一个k记录等等。
List 会立即将所有记录加载到 Java 堆中。
如果我需要一些后台批处理作业(例如计算分析),我可以使用流操作,因为我不会立即将所有记录加载到堆中。
如果我需要返回包含所有记录的 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) 我正在尝试将 PMD 插件集成到构建阶段的 pom.xml 文件。
3.13.0<?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) 我有一个以 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) 所以我想使用最小的键作为优先级,然后返回对应键的值:
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++ 中工作正常。
uWSGI 可以同时作为 Web 服务器和应用程序服务器吗?
例如,独立的 WSGI 容器 https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/ 但同样,它建议使用 HTTP 服务器。为什么?uWSGI 不能处理 HTTP 请求吗?
我阅读了有关部署 Flask 应用程序的不同文章。他们说,我需要 uWSGI 和 nginx——一种流行的选择。
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) 我有 Notepad++ 7.5.8 (npp.7.5.8)。它没有插件管理器;以前的版本曾经有它。我遵循了这些说明。
我是从https://sourceforge.net/projects/npp-compare/下载的
ComparePlugin.readme.txt
安装ComparePlugin.dll到插件目录C:\Program Files\Notepad++\Plugins
我没工作。插件菜单没有比较插件。虽然它有其他插件。
我使用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 依赖项。如果没有其他工作,手动下载东西是我要做的最后一件事。
我尝试使用 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) Groovy 具有tap和with功能。它们之间有什么区别
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) java ×5
jackson ×2
maven ×2
collections ×1
file-upload ×1
flask ×1
groovy ×1
http ×1
java-10 ×1
java-14 ×1
json ×1
keyvaluepair ×1
nginx ×1
notepad++ ×1
pmd ×1
python-3.x ×1
spring ×1
spring-boot ×1
spring-data ×1
uwsgi ×1
xml ×1