Home > front end >  Where is the problem in my corde(YML file) or Azure database? Spring Boot and Azure
Where is the problem in my corde(YML file) or Azure database? Spring Boot and Azure

Time:07-05

Fist I am start project and create Azure database. After that DB link to my project and it was run. But it is not and indicate run error->

22:40:19.125 [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

Where is the problem in my corde(YML file) or Azure database?

application.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>

Employer.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(Main Class)

    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);
    }

}

indicate error

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

CodePudding user response:

The error coming from the YAML parser is misleading - it is not actually username: javatechi that is incorrect.

Please have a read through the YAML spec, 2.1 Collections and its examples, which is introduced with:

2.1. Collections

YAML’s block collections use indentation for scope and begin each entry on its own line. Block sequences indicate each entry with a dash and space (“- ”). Mappings use a colon and space (“: ”) to mark each key/value pair. Comments begin with an octothorpe (also called a “hash”, “sharp”, “pound” or “number sign” - “#”).

In other words it is actually the previous line that is incorrect:

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

You can fix it by adding a space after the url: key to seperate it from its value:

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