小编TV *_*ath的帖子

使用AsyncRestTemplate多次创建API并等待所有内容完成

我必须使用RestTemplate多次使用不同的参数进行Rest API调用.API是相同的,但它是正在改变的参数.次数也是可变的.我想使用AsyncRestTemplate但我的主线程应该等到所有API调用都成功完成.我还想处理每个API调用返回的响应.目前我正在使用RestTemplate.基本形式如下.

List<String> listOfResponses = new ArrayList<String>();
for (Integer studentId : studentIdsList) {
    String respBody;
    try {
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, requestEntity, String.class);
    } catch (Exception ex) {
        throw new ApplicationException("Exception while making Rest call.", ex);
    }
    respBody = requestEntity.getBody();
    listOfResponses.add(respBody);          
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下如何实现AsyncRestTemplate?

java spring multithreading future asyncresttemplate

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

按相关实体筛选休眠条件

我有一个包含相关实体集合的实体。

public class Student{

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "COURSE_STUDENT_ID" , insertable=false,updatable=false)
private Set <Course> courses;
Run Code Online (Sandbox Code Playgroud)

我想按课程名称和学生班级 ID 过滤学生。现在我已经弄清楚了如何按类 id 过滤,但我不知道如何按 courseId 过滤,因为 Student 实体有一组课程并且表是相关的。我已经阅读了一些文章,但没有任何代码与我已经读过的代码相匹配。

CriteriaBuilder criteriaBuilder = persistenceStore.createCriteriaBuilder();
CriteriaQuery<Object> criteria = criteriaBuilder.createQuery();
Root<Student> root = criteria.from(Student.class);
List<Predicate> params = new ArrayList<Predicate>();

params.add(criteriaBuilder.equal(root.get("classId"),classId));

Predicate[] predicates = new Predicate[params.size()];
params.toArray(predicates);
criteria.select(root);
criteria.where(criteriaBuilder.and(predicates));
Query query = persistenceStore.createQuery(criteria);
List<Student> resultList = query.getResultList();
Run Code Online (Sandbox Code Playgroud)

java hibernate criteria-api jakarta-ee

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

Spring Data - 无法在服务层中捕获 DataIntegrityViolationException

我正在使用 org.springframework.data.jpa.repository.JpaRepository 保存方法来保存实体。我认为 save 方法来自 CrudRepository 接口。我正在从服务类调用此保存方法。

try {
    studentLog = studentsLogRepository.save(studentLog);        
} catch (DataIntegrityViolationException dive) {
    LOGGER.warn("Constraint violation occurred. Cannot insert the same record twice.", dive);
}
Run Code Online (Sandbox Code Playgroud)

但问题是DataIntegrityViolationException没有在 catch 块中被卡住。相反,我在以下日志中看到。

java.sql.BatchUpdateException: ORA-00001: unique constraint (QA_VPP.UX_TVPPC_TRAN_LOG_1) violated
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10296) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
        at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:216) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) ~[commons-dbcp-1.3.jar:1.3]
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) ~[commons-dbcp-1.3.jar:1.3]
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.6.7.Final.jar:3.6.7.Final]...
Run Code Online (Sandbox Code Playgroud)

2016-12-28 10:13:52,655 LL="DEBUG" CR="1_1482920032_407_357_l73q069_VPP" RE="1482920032407" DE="1" TR="tomcat-http--12" LN="o.s.o.j.JpaTransactionManager"  Initiating transaction rollback after commit exception
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update; nested exception is …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data spring-data-jpa

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

无法使用Java删除文件夹

我试图删除只有文件但没有子文件夹但没有成功的文件夹.

码:

File rowFolder = new File(folderPath);
String[] files = rowFolder.list();
for (String file : files){
    File deleteFile = new File(file);
    System.out.println("deleting file -"+deleteFile.getName());
    deleteFile.delete();
}
System.out.println("deleting folder -"+rowFolder.getName());
rowFolder.delete();
Run Code Online (Sandbox Code Playgroud)

输出:

deleting file -testing.pdf
deleting file -app_json.json
deleting file -photo.jpg
deleting folder -bundle_folder
Run Code Online (Sandbox Code Playgroud)

代码不会删除任何文件夹或任何文件.这是为什么?

java file-io file java-io

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

使用JavaMail api发送的电子邮件中未设置发件人

我有以下代码.但在电子邮件中,我将发件人视为" 未知发件人 ".当接收者是gmail和yahoo时,根本不接收电子邮件.我设法从自己的域邮箱(在Outlook中设置)和另一个公司电子邮件地址(不同的域)收到电子邮件.在这两个中,发件人电子邮件被显示但仍然在Outlook中,当收到电子邮件时,它称为"未知发件人"并且名称未显示.

在此输入图像描述

Properties props = new Properties();
        props.put("mail.smtp.host", "mail.domain.com"));

    props.put("mail.smtp.auth","false"));
    props.put("mail.smtp.starttls.enable","false"));
    props.put("mail.smtp.port","25"));

    Session session = Session.getDefaultInstance(props);


    try {
        String subject = "Email Subject";
        MimeMessage message = new MimeMessage(session);
        message.setSender(new InternetAddress("no-reply@domain.com", "Sender Name"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emailTo));
        message.setSubject(subject);
        String content = writer.toString();
        message.setContent(content, "text/html; charset=UTF-8");

        Transport.send(message);
        logger.debug ("Email sent");
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

没有例外,我得到日志"发送电子邮件"

我没有邮箱.我想发送没有发件人的电子邮件,仍然在电子邮件中显示发件人姓名,发件人电子邮件地址为"no-reply@domain.com"

以下是它的显示方式.我有白色标记的公司敏感数据.domain.com也是模拟域名.在实际应用程序中,我使用真实域,但结果是相同的.

在此输入图像描述

为什么gmail和雅虎阻止我的邮件?

java email jakarta-mail html-email

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

Tiff 的 getImageWritersByFormatName 中的问题。获取图像编写器

我正在尝试将 PDF 转换为 tif 图像。我使用以下代码按格式获取图像编写器。

Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("TIFF");

        if (writers == null || !writers.hasNext()) {
            throw new ImageWritingException();
        }
Run Code Online (Sandbox Code Playgroud)

当我在 Eclipse 中独立运行应用程序时,这很好用。但是当我将应用程序部署到 linux 中的 tomcat 服务器时,writers==null是假的,但!writers.hasNext是真导致抛出异常。

我使用 maven 将项目构建为战争。

我在 pom 文件中有以下依赖项。

<dependency>
            <groupId>org.icepdf</groupId>
            <artifactId>icepdf-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai_imageio</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai-codec</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.media</groupId>
            <artifactId>jai_core</artifactId>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

两种环境之间有什么区别?我该如何解决这个问题?

java tomcat tiff image maven

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

PL SQL遍历ID列表

我有一个名字清单。

约翰,山姆,彼得,杰克

我想查询与上面的每个过滤器相同的SQL。每个查询都会给我一个唯一的员工ID,我想用它删除其他一些记录。

select emp_id from employee where emp_name like '%john%';
Run Code Online (Sandbox Code Playgroud)

假设第一个查询的ID为1001。因此删除查询如下。

delete from account_details where emp_id = 1001;
delete from hr_details where emp_id = 1001;
delete from pay_role_details where emp_id = 1001;
Run Code Online (Sandbox Code Playgroud)

对于员工列表,我必须重复此操作。伪代码如下所示。

var emp_list = ['john', 'jack', 'kate', 'peter', 'sam',...]

for each :employee_name in emp_list
    select emp_id as :var_emp_id from employee where emp_name like '%:employee_name%'; 

    delete from account_details where emp_id = :var_emp_id;
    delete from hr_details where emp_id = :var_emp_id;
    delete from pay_role_details where emp_id = :var_emp_id;
end loop …
Run Code Online (Sandbox Code Playgroud)

sql oracle plsql plsqldeveloper oracle-sqldeveloper

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