小编典典

我的代码(YML 文件)或 Azure 数据库中的问题在哪里?Spring Boot 和 Azure

sql

我开始项目并创建 Azure 数据库。在那个数据库链接到我的项目之后,它就运行了。但它不是并且指示运行错误->

22:40:19.125 [main] ERROR org.springframework.boot.SpringApplication - Application run failed

org.yaml.snakeyaml.scanner.ScannerException:在“阅读器”第 4 行第 13 列中不允许映射值:用户名:javatechi

我的代码(YML 文件)或 Azure 数据库中的问题在哪里?

应用程序.yml

    spring:
  datasource:
    url:jdbc:jdbc:sqlserver://xxxx.database.windows.net:1433;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
    username: xxxx
    password: xxxxxxxx
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      dialect: org.hibernate.dialect.SQLServer2012Dialect

server:
  port: 9191

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 https://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.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.javatechie</groupId>
    <artifactId>springboot-azuresql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-azure-sql</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.webjars.npm</groupId>
            <artifactId>table</artifactId>
            <version>5.4.6</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

雇主.java

    package com.javatechie.azuresql;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private  String dept;
    private long salary;
}

**EmployeeRepository.java**

package com.javatechie.azuresql;

import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
}

SpringbootAzuersqlApplication.java(主类)

    package com.javatechie.azuresql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
@RestController
public class SpringbootAzuresqlApplication {

    @Autowired
    private EmployeeRepository repository;

    @PostMapping("/product")
    public Employee addEmployee(@RequestBody Employee employee){
        return repository.save(employee);
    }

    @GetMapping("/products")
    public List<Employee> getEmployees(){
        return repository.findAll();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAzuresqlApplication.class, args);
    }

}

指示错误

21:55:28.055 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
 in 'reader', line 4, column 13:
        username: javatechi
                ^
  [1]: https://i.stack.imgur.com/dfEgg.jpg

阅读 165

收藏
2022-07-22

共1个答案

小编典典

来自 YAML 解析器的错误具有误导性——它实际上并不是username: javatechi不正确的。

请阅读YAML 规范 2.1 Collections及其示例,其中介绍了:

2.1。收藏品

YAML 的块集合使用缩进来表示范围,并在每个条目自己的行开始。块序列用破折号和空格 ( “- ”) 表示每个条目。映射使用冒号和空格 ( “: ”) 来标记每个键/值对。注释以 octothorpe 开头(也称为“hash”、“sharp”、“pound”或“number sign”- “#”)。

换句话说,实际上是上一行是不正确的:

    url:jdbc:jdbc:sqlserver://xxxx.database.windows.net:1433;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;

您可以通过在键后添加一个空格url:来将其与其值分开来修复它:

    url: jdbc:jdbc:sqlserver://xxxx.database.windows.net:1433;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
2022-07-22