本文介绍了如何查看我的数据库H2的数据,为什么localhost:8080 / h2_console不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看数据库h2中的数据。我不知道我对
有什么问题,我在文件.properties

I want to see the data in my database h2. I do not know what I'm wrong aboutI have this configuration in file .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

h2_console rel = nofollow noreferrer> http:// localhost:8080 / h2_console
i出现此错误:

when i make a request in chrome http://localhost:8080/h2_consolei get this error:

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 / 我从控制器
Hello World!

When I use this http://localhost:8080/user/ I get this message from the controllerHello World!

这是我的项目路径:

C:\Users\DELL\Desktop\txts\aquiesta\springTeam\traslateInClick\src\main\resources\application.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>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>


推荐答案

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

According to the code below looks like your context path is also "" (blank).

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

控制器类,问题仅在于您的URL映射。您的URL / h2_console getBook / {id} URL冲突c $ c>方法。

And according to code in the controller class, the issue is with your URL mapping only. Your URL /h2_console is getting conflicted with /{id} URL of getBook method.

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

我建议您为应用程序添加正确的上下文路径,以便您的应用程序URL不会与 h2-console URL冲突。上面的配置有时会运行,而其他时候会失败。如果注册了h2数据库上下文URL,您的控制台将打开;如果注册失败,您将得到当前正在获得的错误页面。

I would suggest you add the correct context path for your application so that your application URL will not get conflicted with h2-console URL. The above configurations are sometimes running and failing other times. Your console will open if h2 database context URL gets registered, if it fails to get register, you will get the error page you are getting right now.

application.properties(注释其他h2属性,因为它们是默认属性)

application.properties (Commenting other h2 attributes as they are default)



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





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

因此,您的最终到达网址为:

So your final URLs will be :

要获取该书: {id} /

To get the book: http://localhost:8080/book/{id}/

要创建一本书,请执行以下操作: {name} / author / {id} /

To create a book: http://localhost:8080//create/{name}/author/{id}/

要致电回家:

要访问h2数据库控制台:

To access h2 DB Console: http://localhost:8080/h2_console/login.jsp

这篇关于如何查看我的数据库H2的数据,为什么localhost:8080 / h2_console不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:15