小编sam*_*cde的帖子

Spring 数据 JPA existsByField 在 MySQL 中不区分大小写,如何使其区分大小写

ClientsRepository 类

public interface ClientsRepository extends JpaRepository<ClientsEntity, Long> {

    boolean existsByClientId(String clientId);

}
Run Code Online (Sandbox Code Playgroud)

ClientsEntity 类

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "clients")
public class ClientsEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String clientId;

}
Run Code Online (Sandbox Code Playgroud)

客户表
| id | client_id | |---------------------|------------------| | 1 | ABC | |---------------------|------------------|

调用existsByClientId("abc") 时它返回true,如何强制检查大小写?

实际结果:
existsByClientId("abc") --> true
existsByClientId("ABC") --> true

预期结果:
existsByClientId("abc") --> false
existsByClientId("ABC") --> true

Java 版本 8
Spring Boot 版本 2.1.2.RELEASE
mysql-connector-java 版本 5.1.46

java mysql spring jpa spring-data-jpa

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

如何使用 Java 进行拓扑排序(依赖解析)

描述

该问题的目的是实现一个接口,该接口将根据任务的依赖关系信息对任务列表进行排序。例如,如果任务 A 依赖于任务 B 和 C,则这意味着要开始处理任务 A,必须首先完成任务 B 和 C。我认为它应该像有向图结构。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The task class represents a certain activities that must be done as the part of the project planning
 */
class Task {

    /**
     * Unique name of the activity
     */
    private String name;

    /**
     * A list of names of the activities that must be completed in order to be able to start the current activity
     */
    private List<String> predecessors;

    public …
Run Code Online (Sandbox Code Playgroud)

java graph directed-graph jgrapht guava

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

为什么 Spring Data JPA 规范接口扩展了 Serialized?

我很好奇为什么Spring/JPA要做Specification接口Serializable?我知道 jpa 实体应该是可序列化的,并且 jpaSpecification使用这些实体。但Specification只是实现查询生成器,为什么要这样做呢Serializable?-

public interface Specification<T> extends Serializable 
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data-jpa

5
推荐指数
0
解决办法
388
查看次数

将列表总结为流的限制

如何获取记录,计数总和应该在限制内。在下面的示例中,Records对象包含 recordId 和计数,我想根据计数的总和应该小于或等于我的限制条件来获取记录数据。

public class Records {
    private int recordID;
    private int count;

    public Records(int recordID, int count) {
        this.recordID = recordID;
        this.count = count;
    }

    public int getRecordID() {
        return recordID;
    }

    public void setRecordID(int recordID) {
        this.recordID = recordID;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}


public static void main(String[] args) {
    
    final List<Records> recordList = new ArrayList<>();
    
    recordList.add(new Records(100, 10));
    recordList.add(new Records(501, 20));
    recordList.add(new …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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

带 websocket 和邮递员的 Spring Boot

我已经根据教程创建了测试项目

@Configuration
@EnableWebSocketMessageBroker
public class WSConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/chat");
        registry.addEndpoint("/chat").withSockJS();
    }

@Controller
public class WSController {

    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public String send(String message) throws Exception {
        return "test";
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在努力与邮递员联系,我尝试了不同的网址:

  • ws://本地主机:8080

  • ws://localhost:8080/应用程序

  • ws://localhost:8080/app/chat

每次我单击连接按钮时,都会收到 404 作为响应我做错了什么?(我是 websocket 新手)

spring-boot spring-websocket postman

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

这段代码可以避免 Android 处理程序内存泄漏吗?

handler1 是泄漏。

我想将handler1代码转换为handler2代码。这可以吗?

这两个代码有什么区别?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // leaks!
        Handler handler1 = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("LOG", "Hello~1");
            }
        };

        Handler handler2 = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                Log.e("LOG", "Hello~2");
                return false;
            }
        });

        handler1.postDelayed(new Runnable() {
            @Override
            public void run() { }
        }, 60000);
        handler2.postDelayed(new Runnable() {
            @Override
            public void run() { } …
Run Code Online (Sandbox Code Playgroud)

android memory-leaks callback handler

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

mvn jetty:运行失败 原因:java.lang.RuntimeException:扫描条目 META-INF/versions/9/javax/xml/bind/ModuleUtil.class 时出错

我正在尝试对我的项目执行mvn jetty:run,但是,该过程不断失败,并且我无法让项目在本地运行。从错误堆栈跟踪来看,我认为值得关注的一个相关问题是Caused by: java.lang.RuntimeException: Error scanning entry META-INF/versions/9/javax/xml/bind/ModuleUtil.class from jar file:///C:/Users/Tabish/.m2/repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar. 我尝试过更改 jaxb 的版本但无济于事。

这是错误跟踪

[WARNING] Failed startup of context o.e.j.m.p.JettyWebAppContext@4998e74b{/myntra-v4-proxy,[file:///C:/Users/Tabish/Documents/Increff/Code/proxies/myntra-v4-proxy/target/webapp-tmp/, jar:file:///C:/Users/Tabish/.m2/repository/io/springfox/springfox-swagger-ui/2.4.0/springfox-swagger-ui-2.4.0.jar!/META-INF/resources],STARTING}{file:///C:/Users/Tabish/Documents/Increff/Code/proxies/myntra-v4-proxy/target/webapp-tmp/}
org.eclipse.jetty.util.MultiException: Multiple exceptions
    at org.eclipse.jetty.annotations.AnnotationConfiguration.scanForAnnotations (AnnotationConfiguration.java:536)
    at org.eclipse.jetty.annotations.AnnotationConfiguration.configure (AnnotationConfiguration.java:447)
    at org.eclipse.jetty.webapp.WebAppContext.configure (WebAppContext.java:491)
    at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1336)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:772)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:263)
    at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:517)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart (JettyWebAppContext.java:398)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:114)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:61)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart (ContextHandlerCollection.java:161)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:114)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:61) …
Run Code Online (Sandbox Code Playgroud)

java spring pom.xml maven

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

使用条件从另一个列表中删除嵌套列表中的元素 - Java 8

我想从具有来自另一个列表的条件值的列表中删除项目。这是对象

public class Student{
    
    private String name;
    private String age;
    private List<Course> listCourses;
    
    //Setters and getters
    
}
    
public Class Course{
    
    private String courseName;
    private List<CourseDetail> listCoursesDetail;
    
    //Setters and getters
}
    
public Class CourseDetail{
    
    private String id;
    private String grade;
    private String professor;
    
    //Setters and getters
    
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该对象Student有一个列表,该列表内还有来自对象 CourseDetail 的另一个列表。我想要实现的是过滤或删除 private 中的元素List<CourseDetail> listCoursesDetail,其中 ID 不等于其他对象的 id。

public class Process{
    
    private String name;
    private List<ProcessDetail> listProcessDetail;
    
    //Setters and getters
    
}
    
public class ProcessDetail{

    private String id;
    
    //Setters and …
Run Code Online (Sandbox Code Playgroud)

java list filter java-8 java-stream

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

Spring boot /quartz webapp 不发布“actuator/quartz”端点

我有一个带有石英依赖项的 Spring Boot Web 应用程序,并且正在运行 Spring 执行器。但 Actuator 只发布 14 个端点。我需要启用什么才能让执行器发布石英端点吗?

父项目是

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

并具有以下 spring 依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

以及以下执行器属性:

management.endpoint.shutdown.enabled=true
management.endpoint.quartz.enabled=true
management.endpoints.web.exposure.include=*
Run Code Online (Sandbox Code Playgroud)

尽管存在石英依赖性,但尚未发布针对石英的弹簧执行器端点。/actuator 端点报告以下内容:

{
    "_links": {
        "self": {
            "href": "https://localhost:8080/actuator",
            "templated": false
        },
        "beans": {
            "href": "https://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "https://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "https://localhost:8080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "https://localhost:8080/actuator/health",
            "templated": false
        }, …
Run Code Online (Sandbox Code Playgroud)

java spring quartz-scheduler spring-boot spring-boot-actuator

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

Lombok 生成的构造函数无法在 SpringBootTest 中隐式自动装配

我想通过构造函数将A类注入到B类中。

测试目录中的类结构

@Component
public class A {
}
Run Code Online (Sandbox Code Playgroud)
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@RequiredArgsConstructor
public class B {
    private final A a;

    @Test
    public void testB() {
        System.out.println("testB");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行测试时抛出以下错误。

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [final com.wen.h2.A a] in constructor [public com.wen.h2.B(com.wen.h2.A)].
    at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:200)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameters(ExecutableInvoker.java:183)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:74)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestClassConstructor(ClassBasedTestDescriptor.java:342)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateTestClass(ClassBasedTestDescriptor.java:289)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:79)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:267)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:259)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:258)
    at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:101)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:100)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:65)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:111)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at …
Run Code Online (Sandbox Code Playgroud)

java lombok spring-boot-test

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

如何从 Spring Cloud 流中读取 Kafka 消息密钥?

我正在使用 Spring Cloud 流来消费来自 Kafka 的消息。

是否可以从代码中读取 Kafka 消息密钥?

我有一个 Kafka 主题,它通常有两种类型的消息。要采取的操作因消息键而异。我看到 spring 文档只有以下内容来阅读消息。在这里,我需要指定消息的实际映射(此处为 Greetings 类)。但是,我需要一种方法来读取消息键并确定可反序列化的 Pojo

public class GreetingsListener {

    @StreamListener(GreetingsProcessor.INPUT)
    public void handleGreetings(@Payload Greetings request) {
     
    }
}
Run Code Online (Sandbox Code Playgroud)

spring apache-kafka spring-cloud-stream

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

等于与内存比较问题

public static void main(String[] args) {
  Long l = 500L;
  Long l1 = 500L;
  System.out.println(l == l1);
  User u = new User();
  u.setL(500L);
  User u1 = new User();
  u1.setL(500L);
  System.out.println(u.getL() == u1.getL());
}
Run Code Online (Sandbox Code Playgroud)

上述程序的输出为false,true。为什么它会产生差异输出。

public class User {

    private long l;

    public long getL() {
        return l;
    }

    public void setL(long l) {
        this.l = l;
    }

}
Run Code Online (Sandbox Code Playgroud)

上面的定义是针对用户类的

java

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