小编bob*_*bob的帖子

Spring API 返回 XML 而不是 JSON

我有两个 Spring Boot 项目 - A(带有 API 的主要较大项目)和B (由A作为 pom.xml 中的依赖项导入的库)

项目B的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>

        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.b.root</groupId>
    <artifactId>b</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>b</name>
    <description>Importable Jar</description>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <oauth2.version>2.1.1.RELEASE</oauth2.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <!-- Removing dependency for the embedded Tomcat -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <!-- End -->
            </exclusions>
        </dependency>

        <!-- …
Run Code Online (Sandbox Code Playgroud)

java spring jackson spring-boot jackson-dataformat-xml

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

JPA中如何连接多个数据库?

我有一个 Spring Boot 应用程序,当前使用 JPA 连接到单个数据库。其 application.properties 文件中的连接详细信息:

spring.datasource.url=jdbc:oracle:thin:@localhost:1522:orcl
spring.datasource.username=example
spring.datasource.password=example
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
Run Code Online (Sandbox Code Playgroud)

现在我有一个想要连接的新数据库,因此我的应用程序现在连接了 2 个数据库。我可以使用 JDBC 连接它,在那里我必须手动编写连接代码。

我一直在寻找解决方案。有些处于休眠状态,具有多个配置文件。其他人正在使用 JDBC。

我尝试在 spring 文档中查找是否有在 application.properties 文件中定义多个数据源的规定,但找不到任何内容。

我想知道是否存在单独使用 JPA 的解决方案。谢谢你的时间。

java database jpa spring-data-jpa spring-boot

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

如何以给定格式获取日期?

这是我想要做的:

  1. 取今天的日期(比如 2021-07-07)
  2. 将其格式化为 (2021-07-07T00:00:00.000Z)
  3. 在 API 中发送

我找不到一种简单的方法来转换2021-07-072021-07-07T00:00:00.000Z. 我们当然可以附加T00:00:00.000Z字符串,但我想避免这种情况。

到目前为止,我拥有的每种方法(使用 LocalDate、LocalDateTime、Instant、Date)都涉及使用时区 (ZoneId) 或转​​换为 Epoch,它们最终只给出响应直到几秒钟而不是几毫秒。

我相信一定有更简单的方法。

编辑 1:传递区域 ID 不是问题,我们可以做到。

java java-8

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

是否有 Java 8 方法来填充以下地图?

我有以下 Dto 类:

@AllArgsConstructor
@ToString
@Getter
public class Dto {
    private String cityName;
    private String personName;
}
Run Code Online (Sandbox Code Playgroud)

我想从上面的 Dtos 列表创建一个 Map<String, List>。逻辑是这样的,所有具有相同城市的Dto都应该以该城市名称作为关键字一起列出。可以使用普通的 foreach 轻松完成,但我想知道这是否可以使用 Java 8 完成。

可以使用computeIfAbsent、computeIfPresent、stream.collect(toMap())等来做些什么吗?

public class Runner {
    public static void main(String[] args) {
        Map<String, List<Dto>> map = new HashMap<>();
        List<Dto> tempList = null;

        List<Dto> dtos = Arrays.asList(
                new Dto("Indore", "A"),
                new Dto("Indore", "B"),
                new Dto("Pune", "C"),
                new Dto("Pune", "D")
        );

        for (Dto dto : dtos) {
            if(map.containsKey(dto.getCityName())) {
                map.get(dto.getCityName()).add(dto);
            }
            else {
                tempList = new …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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