【问题标题】:White label error in spring boot !! May be URL doesn't reach to controller弹簧靴中的白标错误!可能是 URL 无法到达控制器
【发布时间】:2020-04-22 22:13:12
【问题描述】:

URL: "localhost:8082/api/notebooks/all" 应该到达控制器的方法,但它显示白标错误并显示应用程序映射问题的消息。请问war包有什么问题吗?

控制器:

    package com.savenotes.controller;

    import java.util.List;
    import java.util.UUID;

    import javax.validation.Valid;
    import javax.validation.ValidationException;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import com.savenotes.model.Notebook;
    import com.savenotes.repository.NotebookRepository;
    import com.savenotes.savenotes.Mapper;
    import com.savenotes.viewmodel.NotebookViewModel;


    @RestController
    @RequestMapping("/api/notebooks")
    @CrossOrigin
    public class NoteBookController 
    {

        @Autowired
        private NotebookRepository notebookRepository;

        @Autowired
        private Mapper mapper;

        public NoteBookController(NotebookRepository notebookRepository, Mapper mapper)
        {
            this.notebookRepository = notebookRepository;
            this.mapper = mapper;
        }

        @GetMapping("/all")
        public List<Notebook> fetchAllNotebook() 
        {
            List<Notebook> listOfNotebooks = notebookRepository.findAll();
            System.out.println("Hiiiiiiii");
            return listOfNotebooks;
        }

        @PostMapping
        public Notebook save(@Valid @RequestBody NotebookViewModel notebookViewModel, 
                                    BindingResult bindingResult)
        {
            if(bindingResult.hasErrors())
            {
                throw new ValidationException();
            }
            Notebook notebook = mapper.convertToNotebookEntity(notebookViewModel);

            notebookRepository.save(notebook);
            return notebook;
        }

        @DeleteMapping("/{id}")
        public void delete(@PathVariable String id)
        {
            notebookRepository.deleteById(UUID.fromString(id));
        }
    }

在 application.properties 文件中给出了端口号。

Application.properties:

spring.datasource.url=jdbc:h2:file:./asdj.db
noteit.db.recreate=true

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=
spring.mail.password=
server.port=8082

这个项目是在与 java8 版本的战争中创建的。我想我已经包含了所有必要的依赖项

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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.savenotes</groupId>
    <artifactId>savenotes</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>savenotes</name>
    <description>Project to save notes</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

【问题讨论】:

    标签: java spring-boot url model-view-controller war


    【解决方案1】:

    您的构建器是一场战争,因此您必须根据您的 pom 文件使用模块名称,看起来构建器的最终名称是 savenotes-0.0.1-SNAPSHOT.所以您必须按以下方式调用 api

    localhost:8082/0.0.1-SNAPSHOT/api/notebooks/all

    如果你改变 pom.xml 中的构建部分,如下所示

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

    你可以调用 api 作为 localhost:8082/savenotes/api/notebooks/all

    【讨论】:

    • 实际上我的主类在包名“com.savenotes.savenote”下。我的朋友建议我只保留包名“com.savenote”,现在它可以正常工作了。
    猜你喜欢
    • 2021-09-18
    • 2015-07-04
    • 2019-07-10
    • 1970-01-01
    • 2018-02-14
    • 2018-02-16
    • 2017-12-22
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多