使用嵌入式 tomcat 未在 Spring Boot 中映射请求

Posted

技术标签:

【中文标题】使用嵌入式 tomcat 未在 Spring Boot 中映射请求【英文标题】:Request are not being mapped in spring boot using embedded tomcat 【发布时间】:2019-12-16 03:33:19 【问题描述】:

我已经使用 mysql 启动了一个新的 REST spring boot 应用程序,但我无法使用 postman 注册用户并且无法映射它们

我使用的是 STS 4.3.1, 用于 MySQL 的 xampp, 我已经看过每个创建 REST 服务的示例。

//我的入口点

package com.accounts.rtgsneft;


@SpringBootApplication(scanBasePackages = "com.account.rtgsneft.controllers","com.account.rtgsneft.dao","com.account.rtgsneft.models","com.account.rtgsneft.services")
public class RtgsNeftApplication 

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


//我的控制器

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/")

public class EmployeeController 
    @Autowired
    private EmployeeService employeeService;



    @PostMapping("/register")
    public ResponseEntity<?> RegisterControl(@RequestBody EmployeeMaster employee) 
        System.out.println("here:");
        try 
            employee = employeeService.Register(employee);
         catch (Exception e) 
            System.out.println(e.getMessage());
        

        return ResponseEntity.ok(employee);
    

    @RequestMapping("/error")
    public void getError() 
        System.out.println("error caught");
    


//我的模型

@Component
@Entity
@Table(name = "users")
public @Getter @Setter class EmployeeMaster 

    @Id
    private Long empNumber;
    private String empName;
    private String username;
    private String empPassword;
    private String email;
    private String branch;
    private String role;

    @Override
    public String toString() 
        return "EmployeeMaster [empNumber=" + empNumber + ", empName=" + empName + ", username=" + username
                + ", empPassword=" + empPassword + ", email=" + email + ", branch=" + branch + ", role=" + role + "]";
    



//我的仓库

package com.accounts.rtgsneft.repositories;

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeMaster, String> 


//我的服务接口和类

package com.accounts.rtgsneft.sevices;


public interface EmployeeService 


    public EmployeeMaster Register(EmployeeMaster employeeMaster);



package com.accounts.rtgsneft.sevices;


@Component
public class EmployeeServiceImpl implements EmployeeService 

    @Autowired
    private EmployeeRepository employeeRepo;
    @Override
    public EmployeeMaster Register(EmployeeMaster employeeMaster) 

        return employeeRepo.save(employeeMaster);
    




//我的应用程序.properties

spring.datasource.url=jdbc:mysql://localhost:3306/rtgs_neft?useSSL=false&useTimezone=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.jpa.show-sql=true

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = update

server.port= 8080

//控制台

2019-08-08 17:06:28.132  INFO 9584 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
  2019-08-08 17:06:28.134  INFO 9584 --- [           main] c.accounts.rtgsneft.RtgsNeftApplication  : Started RtgsNeftApplication in 3.706 seconds (JVM running for 4.289)
    2019-08-08 17:06:59.576  INFO 9584 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2019-08-08 17:06:59.576  INFO 9584 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2019-08-08 17:06:59.600  INFO 9584 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 24 ms

在控制台上没有错误,servlet 已成功分派。 但是当我使用邮递员发布请求时,我无法在 servlet 上发布我的数据

localhost:8080/employee/register

【问题讨论】:

您是否收到异常或错误消息? 【参考方案1】:

您尝试访问的端点配置不正确。 请替换根@RequestMapping("/")@RequestMapping("/employee")。 现在点击这个端点localhost:8080/employee/register它会工作。

【讨论】:

以上是关于使用嵌入式 tomcat 未在 Spring Boot 中映射请求的主要内容,如果未能解决你的问题,请参考以下文章

JSP 文件未在 Spring Boot Web 应用程序中呈现

Spring Boot 未在 Eclipse 中与 Tomcat 一起运行

spring boot 应用程序未在 Tomcat 中配置的端口号上启动

spring-boot 应用程序的外部配置

具有嵌入式 init.d 脚本的 Spring Boot 应用程序未在重新启动时启动

Spring Boot嵌入式Tomcat能否访问定义的spring.tomcat.basedir的webapps文件夹