【问题标题】:How see the data of my data base H2, why localhost:8080/h2_console not work?如何查看我的数据库 H2 的数据,为什么 localhost:8080/h2 控制台不起作用?
【发布时间】:2026-01-01 04:40:01
【问题描述】:

我想查看我的数据库 h2 中的数据。我不知道我错了什么 我在文件 .properties 中有这个配置

spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto = update

#spring.jpa.hibernate.hbm2ddl.auto:validate

当我在 chrome 中提出请求时http://localhost:8080/h2_console 我收到此错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri May 10 00:37:28 BOT 2019
There was an unexpected error (type=Not Found, status=404).
No message available

这是我的控制器:

package com.example.demo.controller;

import com.example.demo.model.Friendship;
import com.example.demo.model.User;
import com.example.demo.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping({"/user"})
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;
    static Logger log = Logger.getLogger(UserController.class.getName());

   @GetMapping(path = {"/"})
public Meet getBook() {
    log.info("HELLO WORDL");
    return null;
} 
    @PostMapping("/create")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        log.info("CREATE");
        return new ResponseEntity<User>(userServiceImpl.createUser(user), HttpStatus.OK);
    }


    @PostMapping("/addFriend")
    public ResponseEntity<User> getUser(@RequestParam("owner") Long ownerId, @RequestParam("friend") Long friendId) {
        return new ResponseEntity<User>(userServiceImpl.createLinkWithFriends(ownerId, friendId), HttpStatus.OK);
    }

    @GetMapping("/owner/{id}/friends")
    public ResponseEntity<List<User>> getFriendsOf(@PathVariable("id") Long ownerId) {
        return new ResponseEntity<List<User>>(userServiceImpl.getFriendsOf(ownerId), HttpStatus.OK);
    }

    @GetMapping("/getUsers")
    public ResponseEntity<List<User>> getUsers() {
        log.info("getUser");
        return new ResponseEntity<>(userServiceImpl.getUsers(), HttpStatus.OK);
    }

    @GetMapping("/showFriends/{id}")
    public ResponseEntity<List<User>> getUsers(@PathVariable("id")  int id) {
        log.info("showFriends"+id);
        return new ResponseEntity<>(userServiceImpl.getUsers(), HttpStatus.OK);
    }

}

当我使用这个http://localhost:8080/user/ 时,我从控制器收到这条消息 世界你好!

这是我的项目路径:

C:\Users\DELL\Desktop\txts\aquiesta\springTeam\traslateInClick\src\main\resources\application.properties

这是我的回购https://github.com/hubmanS/traslateInClick

这是我的依赖。

<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>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

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

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

【问题讨论】:

  • 你能告诉我你的实体吗?我认为您的实体中存在不兼容的数据类型。也许您首先使用字段字符串创建实体,然后将其更改为 Integer。它只会在您的 Java 对象中发生变化,而不会在数据库中发生变化。
  • @ToePyaeSoneOo 我编辑了我的问题。请参阅错误
  • 您能否显示您的应用程序属性文件的路径?
  • @XeshanJ 我编辑了我的问题,请参阅
  • @yaha 你能在这里分享你的整个 Spring Boot 控制台日志吗?

标签: java spring jpa console h2


【解决方案1】:

根据下面的代码,您的上下文路径似乎也是“”(空白)。

@RestController
@RequestMapping({"/"})
public class MeetController{
...
}

根据控制器类中的代码,问题仅在于您的 URL 映射。您的 URL /h2_console/{id} URL 的 getBook 方法发生冲突。

@GetMapping(path = {"/{id}"})
    public Meet getBook(@PathVariable("id") int id) {
        return null;
    }

我建议您为您的应用程序添加正确的上下文路径,这样您的应用程序 URL 就不会与 h2-console URL 发生冲突。上述配置有时会运行,有时会失败。如果 h2 数据库上下文 URL 已注册,您的控制台将打开,如果注册失败,您将看到您现在得到的错误页面。

推荐解决方案:

application.properties(将其他 h2 属性注释为默认)

spring.h2.console.enabled=true
#spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:file:~/test
#spring.datasource.username=sa
#spring.datasource.password=
#spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
#spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto = update

MeetController:

package com.example.demo.controller;

import com.example.demo.model.Meet;
import com.example.demo.service.MeetServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"/book"})
public class MeetController {

    @Autowired
    MeetServiceImpl bookService;

    @RequestMapping("")
    public String home(){
        return "Hello World!";
    }

    @GetMapping(path = {"/{id}"})
    public Meet getBook(@PathVariable("id") int id) {
        return null;
    }

    @PostMapping("/create/{name}/author/{id}")
    public ResponseEntity<Meet> createBook() {
        return null;
    }
}

所以您的最终到达网址将是:

获取书:http://localhost:8080/book/{id}/

要创建一本书:http://localhost:8080//create/{name}/author/{id}/

打电话回家:http://localhost:8080/book/

访问 h2 数据库控制台:http://localhost:8080/h2_console/login.jsp

【讨论】:

  • 我修改了这个@RequestMapping({"/book"}),但是结果是一样的
【解决方案2】:

您的某个 GET 方法导致 URL 映射冲突

@GetMapping(path = {"/{id}"})
public Meet getBook(@PathVariable("id") int id) {
    return null;
}

h2_console 将作为字符串映射到 getBook 方法,并且由于您的方法只接受 int 而引发异常。 With RESTful naming strategy in mind,您应该将映射更改为:

@GetMapping(path = {"/books/{id}"})
public Meet getBook(@PathVariable("id") int id) {
    return null;
}

【讨论】:

  • 所有端点映射良好,但我如何查看 h2-bd 的数据?