404-not-found-while-running-spring-boot-rest-api

Posted

技术标签:

【中文标题】404-not-found-while-running-spring-boot-rest-api【英文标题】: 【发布时间】:2018-04-07 10:28:53 【问题描述】:

运行 springboot restservice 应用程序时出现 404 错误。我正在使用弹簧靴,球衣休息。我尝试了 GET 请求 http://localhost:8080/dunames 但无法解决。请帮忙。

模型类:

@Entity
@Table(name = "du")
public class duname 


    private String duname;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id; 
    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public duname()
    

    

    public duname(String duname, int id)
    
    this.duname=duname; 
    this.id=id;
    


    public String getDuname() 
        return duname;
    

    public void setDuname(String duname) 
        this.duname = duname;
    

服务类:

public class duservice 

    @Autowired
    private durepo durepository;

    public List<duname> getAlldu()
    
        List<duname> dunames=new ArrayList<duname>();
        durepository.findAll()
        .forEach(dunames::add);
        return dunames;
    

    public void addDu(duname dunames)
    
        durepository.save(dunames);
    

    public duname getDu(int id)
    
        return durepository.findOne(id);
    

    public void deleteDu(int id)
    
        durepository.delete(id);
    

    public void updateDu(int id, duname dunames)
    
        durepository.save(dunames);
    

控制器类:

    @RestController
public class ducontroller 


    @Autowired
    private duservice duService;

    private final Logger log=(java.util.logging.Logger) LoggerFactory.getLogger(ducontroller.class);


    @RequestMapping("/dunames")
    public List<duname> getAlldu()
    
          log.info("Starting");
    return duService.getAlldu();
    

    @RequestMapping("/duname/id")
    public duname getdu(@PathVariable int id)
    
        return duService.getDu(id);
    

    @RequestMapping(method=RequestMethod.POST,value="/dunames")
    public void addDuname(@RequestBody duname dunames)
    
        duService.addDu(dunames);
    

存储库:

public interface durepo extends CrudRepository<duname,Integer> 



主类:

 @SpringBootApplication
public class DuaddApplication 

    public static void main(String[] args) 
        SpringApplication.run(DuaddApplication.class, args);
    

Pom 文件:

 <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.du</groupId>
    <artifactId>duadd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>duadd</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>

应用程序属性:

spring.datasource.url=jdbc:postgresql://localhost:2280/todo
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.generate-ddl=true
server.port=8080 

一些控制台输出:

2017-10-26 11:06:08.952  INFO 4552 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-26 11:06:08.975  INFO 4552 --- [           main] com.soprasteria.du.DuaddApplication      : Started DuaddApplication in 7.096 seconds (JVM running for 7.633)
2017-10-26 11:06:32.779  INFO 4552 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-10-26 11:06:32.779  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-10-26 11:06:32.800  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms

【问题讨论】:

是的,我知道但不知道我哪里出错了。 根据您的控制台输出,似乎没有调用方法getAlldu()。那么,您在调用http://localhost:8080/dunames 后收到任何消息了吗? 你的web.xml是什么样的? @LHCHIN 是的,它没有被调用。我收到了 404 消息。 @Wabi 它没有 web.xml 。它是 Spring Boot 应用程序 【参考方案1】:

我得到了解决方案。这是由于包裹的可见性。 主类无法找到控制器类所在的包。 所以,我在同一个包下添加了所有类。您还可以将应用程序类提高一级。

从以下链接获得帮助:

Spring Boot: Cannot access REST Controller on localhost (404)

【讨论】:

以上是关于404-not-found-while-running-spring-boot-rest-api的主要内容,如果未能解决你的问题,请参考以下文章