弹簧靴中的白色标签错误!可能是网址未到达控制器

问题描述 投票:0回答:1

URL:“ localhost:8082 / api / notebooks / all”应该到达控制器的方法,但是它显示白色标签错误,并带有一条消息,明确指出了应用程序映射问题。战争包裹有问题吗?

控制器:

    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个回答
0
投票

您的构建器是一场战争,因此您必须根据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>

您可以以localhost:8082 / savenotes / api / notebooks / all的身份调用api

© www.soinside.com 2019 - 2024. All rights reserved.