Spring Boot 在我的休息控制器上得到404在 Spring 启动?

ryevplcw  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(158)

休息控制器

package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class EmployeeController {

    @Autowired
    EmployeeService empService;

    @RequestMapping(value="/employees", method=RequestMethod.POST)
    public Employee createEmployee(@RequestBody Employee emp) {
        return empService.createEmployee(emp);
    }

    @RequestMapping(value="/employees", method=RequestMethod.GET)
    public List<Employee> readEmployees() {
        return empService.getEmployees();
    }

    @RequestMapping(value="/employees/{empId}", method=RequestMethod.PUT)
    public Employee readEmployees(@PathVariable(value = "empId") Long id, @RequestBody Employee empDetails) {
        return empService.updateEmployee(id, empDetails);
    }

    @RequestMapping(value="/employees/{empId}", method=RequestMethod.DELETE)
    public void deleteEmployees(@PathVariable(value = "empId") Long id) {
        empService.deleteEmployee(id);
    }
}

Spring Boot 主类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class NewsByteApplication {

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

}

控制台日志

2022-10-06 20:02:30.457  INFO 6094 --- [           main] com.example.demo.NewsByteApplication     : Starting NewsByteApplication using Java 17.0.3 on Rohits-MacBook-Air.local with PID 6094 (/Users/rohitgupta/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/NewsByte/target/classes started by rohitgupta in /Users/rohitgupta/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/NewsByte)
2022-10-06 20:02:30.460  INFO 6094 --- [           main] com.example.demo.NewsByteApplication     : No active profile set, falling back to 1 default profile: "default"
2022-10-06 20:02:31.083  INFO 6094 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-10-06 20:02:31.146  INFO 6094 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50 ms. Found 1 JPA repository interfaces.
2022-10-06 20:02:31.623  INFO 6094 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-10-06 20:02:31.634  INFO 6094 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-06 20:02:31.635  INFO 6094 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-06 20:02:31.718  INFO 6094 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-06 20:02:31.718  INFO 6094 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1204 ms
2022-10-06 20:02:31.921  INFO 6094 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-10-06 20:02:31.947  INFO 6094 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.11.Final
2022-10-06 20:02:32.038  INFO 6094 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-10-06 20:02:32.102  INFO 6094 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-10-06 20:02:32.446  INFO 6094 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-10-06 20:02:32.460  INFO 6094 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2022-10-06 20:02:32.855  INFO 6094 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-10-06 20:02:32.865  INFO 6094 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-10-06 20:02:33.098  WARN 6094 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-10-06 20:02:33.485  INFO 6094 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-06 20:02:33.494  INFO 6094 --- [           main] com.example.demo.NewsByteApplication     : Started NewsByteApplication in 3.476 seconds (JVM running for 4.29)

他们都在同一个包里。为什么我的浏览器找不到404,对于localhost:8080。

but5z9lq

but5z9lq1#

您是否尝试过让主类扩展SpringBootServletInitializer?

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class NewsByteApplication extends SpringBootServletInitializer{

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

}

下面是一些关于为什么它工作(假设它工作)Why it is necessary to extendSpringBootServletInitializer while deploying it to an external tomcat

相关问题