如你所知,java.util.Objects是
此类包含用于对对象进行操作的静态实用程序方法.
其中一种方法是Objects.isNull().
我的理解是,Objects.isNull()通过省略第二个,可以消除意外地将空值分配给对象的可能性=.
但是,API Note指出:
此方法存在用作谓词,过滤器(Objects :: isNull)
会不会有任何理由/环境对我应该使用object == null了Objects.isNull()在if语句?
应该Objects.isNull()仅限于Predicates吗?
我试图在PostgreSQL 8.4.2 DB上运行hibernate.每当我尝试运行一个简单的java代码时:
List<User> users = service.findAllUsers();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
PSQLException: ERROR: relation "TABLE_NAME" does not exist
Run Code Online (Sandbox Code Playgroud)
由于我将hibernate.show_sql选项设置为true,我可以看到hibernate正在尝试运行以下SQL命令:
select this_.USERNAME as USERNAME0_0_, this_.PASSWORD as PASSWORD0_0_
from "TABLE_NAME" this_
Run Code Online (Sandbox Code Playgroud)
实际上,它应该至少运行如下:
select this_."USERNAME" as USERNAME0_0_, this_."PASSWORD" as PASSWORD0_0_
from "SCHEMA_NAME"."TABLE_NAME" as this_
Run Code Online (Sandbox Code Playgroud)
有谁知道我需要为Hibernate做些什么改变来为PostgreSQL生成正确的SQL?
我在applicationContext.xml文件中设置了必要的postgreSQL数据源:
<!-- Use Spring annotations -->
<context:annotation-config />
<!-- postgreSQL datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url"
value="jdbc:postgresql://localhost/DB_NAME:5432/SCHEMA_NAME" />
<property name="username" value="postgres" />
<property name="password" value="password" />
<property name="defaultAutoCommit" value="false" />
</bean>
Run Code Online (Sandbox Code Playgroud)
在同一个文件中,我使用PostgreSQL方言设置了会话工厂:
<!-- Hibernate session …Run Code Online (Sandbox Code Playgroud) 我正在尝试将列表拆分为列表,其中每个列表的最大大小为4.
我想知道如何使用lambdas做到这一点.
目前我正在这样做的方式如下:
List<List<Object>> listOfList = new ArrayList<>();
final int MAX_ROW_LENGTH = 4;
int startIndex =0;
while(startIndex <= listToSplit.size() )
{
int endIndex = ( ( startIndex+MAX_ROW_LENGTH ) < listToSplit.size() ) ? startIndex+MAX_ROW_LENGTH : listToSplit.size();
listOfList.add(new ArrayList<>(listToSplit.subList(startIndex, endIndex)));
startIndex = startIndex+MAX_ROW_LENGTH;
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
似乎没有一种简单的方法可以使用lambdas来拆分列表.虽然所有的答案都非常受欢迎,但它们也是lambdas不简化事物的一个很好的例子.
我试图熟悉java.nio.file.Path.relativize()无济于事.
我已经阅读了javadocs,我看过了一些例子.但是,我仍然无法理解以下示例(我使用Linux,向窗口用户道歉):
程序的工作目录是:/ home/userspace/workspace/java8.
有两个文件:/home/userspace/workspace/java8/zoo.txt和/home/userspace/temp/delete/dictionary.txt
以下程序调用Path.relativize():
package certExam.java8.ch9NIO.paths;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Relativize
{
public static void main(String[] args)
{
Path relativePathToZoo = Paths.get("zoo.txt");
Path relativePathToDictionary = Paths.get("../../temp/delete/dictionary.txt");
System.out.println("relativePathToZoo.relativize(relativePathToDictionary): "+relativePathToZoo.relativize(relativePathToDictionary));
System.out.println("relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath(): "+relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath());
System.out.println("relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize(): "+relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize());
System.out.println("relativePathToDictionary.relativize(relativePathToZoo): "+relativePathToDictionary.relativize(relativePathToZoo));
System.out.println("relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize(): "+relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize());
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
relativePathToZoo.relativize(relativePathToDictionary): ../../../temp/delete/dictionary.txt
relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath(): /home/userspace/workspace/java8/../../../temp/delete/dictionary.txt
relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize(): /home/temp/delete/dictionary.txt
relativePathToDictionary.relativize(relativePathToZoo): ../../../../../zoo.txt
relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize(): /zoo.txt
Run Code Online (Sandbox Code Playgroud)
我的问题,我无法理解的是:为什么relativePathToDictionary.relativize(relativePathToZoo)输出../../../../../zoo.txt?
规范化后,它会让你认为zoo.txt存在于根目录中.
relativize()如何解决这样一条深刻的道路?我知道relativize()与当前工作目录相关,因此它会向每个路径添加.. 但是我无法理解,它如何解决了与fonts.txt相关的zoo.txt的路径.
提前致谢,
卢卡斯
我正在测试一个 Spring Batch ItemProcessor,它从数据库中读取值。
根据测试步骤范围组件页面上的建议,我将@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })添加到测试套件注释中:
@ContextConfiguration(locations = { "classpath:spring/context-configuration.xml" })
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomItemProcessorTest
{
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private ItemProcessor<InputClass, OutputClass> customItemProcessor;
@Test
@Sql(scripts = {"insertDataToBeRetrievedByCustomItemProcessor.sql" },
config = @SqlConfig(dataSource = "dataSource"))
public void assertThatCustomItemProcessorProcessorIsRetrievingStillBirthSasEtlObject() throws Exception
{
OutputClass outputObject = customItemProcessor.process(inputClassObject);
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是上面的行:@TestExecutionListeners...正在阻止 @Sql(scripts =... 命令执行。
customItemProcessor 可以轻松地从数据库读取值。
为什么上述注释会阻止 @Sql 脚本运行?我该如何解决这个问题?
在学习微服务的编码练习中,我创建了一个Netflix Zuul项目,用于对微服务进行服务路由。
可悲的是,/ routes端点似乎没有被挂载。一切似乎都正常:定义前缀并为我的服务设置特定的路由。
zuul服务器日志文件上没有错误。当我尝试在邮递员上访问/ routes网址时,出现404错误:

我的Zuul应用程序类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml文件:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <!--should be set to 4.0.0 -->
<groupId>com.booking.system.hotel</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>hotel-gateway-service-server</artifactId>
<name>Hotel Gateway service - zuul</name>
<description>Hotel Gateway service - it uses Netflix Zuul Proxy Server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties> …Run Code Online (Sandbox Code Playgroud) java microservices spring-cloud netflix-zuul spring-cloud-netflix
我有一个Amazon EC2 amzn-ami-hvm-2014.09.2.x86_64-ebs实例正在运行,并且端口443似乎没有打开,即使我已将其添加到实例的安全组(入站和出站设置)中:

我在EC2实例上运行了netstat,未列出端口443:
$ sudo netstat -nupt -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2595/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2061/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2103/sendmail
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 2595/java
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 2595/java
tcp 0 0 :::22 :::* LISTEN 2061/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 1868/dhclient
udp 0 0 172.31.40.1:123 0.0.0.0:* 2080/ntpd
udp 0 …Run Code Online (Sandbox Code Playgroud) 是否有LocalDateFormatter的格式模式来显示当月的基数以及其他值?
例如,我希望打印2016年11月1日,或2017年2月27日.
在此先感谢,卢卡斯
可能重复:
如何比较Java中的字符串?
对于这个相当简单的问题我很抱歉.我有这个非常简单的java程序:
public class ArgIt {
public static void main(String[] args){
if(args[0].equals("x")) System.out.print("x");
if(args[0] == "x") System.out.println("x2 ");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我调用程序> java ArgIt x它只打印一个x.为什么程序在其他任何情况下都不会确认字符串上的==?
java ×9
java-8 ×4
amazon-ec2 ×1
compare ×1
hibernate ×1
https ×1
jodatime ×1
junit4 ×1
lambda ×1
localdate ×1
netflix-zuul ×1
postgresql ×1
spring ×1
spring-batch ×1
spring-cloud ×1
tomcat8 ×1