You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STS4创建Spring Boot微服务POST接口访问返回404问题求助

问题排查与修复方案

核心问题原因汇总

  • 类上@RequestMapping配置错误:Controller类上的@RequestMapping额外配置了method = RequestMethod.POST,类级注解仅需配置路径前缀即可,请求方式由方法级注解指定即可,多余配置会导致路径映射异常
  • 接口路径调用错误:配置的server.servlet.context-path=/demo + Controller类路径/newcall + 方法路径/newResp,完整接口路径应为http://localhost:8080/demo/newcall/newResp,且该接口为POST类型,不能直接用浏览器地址栏(默认GET请求)访问
  • 请求头配置错误:方法上的headers ="Accept=Application/JSON"大小写不符合规范,且属于多余配置,请求头不符合要求时会直接返回404
  • Swagger注解重复冲突:启动类和Swagger配置类重复添加@EnableSwagger2,且SpringFox 3.0.0版本无需手动添加该注解,重复配置会导致Web映射冲突
  • Service手动实例化错误:Controller中直接new Service实现类,会导致Service内部的依赖(如Mybatis Mapper)无法注入,后续会触发空指针异常

具体修复代码

1. 修复DemoController

package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.example.demo.model.MyResponse;
import com.example.demo.service.dao.DemoServiceDAO;

@RestController
@Validated
@RequestMapping("/newcall")
public class DemoController {
  Logger logger =LoggerFactory.getLogger(DemoController.class);

  @Autowired
  private DemoServiceDAO demoServiceDAO;
  
  @PostMapping(value ="/newResp", produces="application/json")
  public MyResponse newResp(@RequestBody String myInput) {
    MyResponse myResponse = null;
    try {
      logger.debug("Started getValue() method...");
      myResponse=demoServiceDAO.getValue(myInput);
      logger.debug("Completed getValue() method...");
    }
    catch(Exception e){
      logger.debug("Exception occurs",e);
      e.printStackTrace();
    }       
    return myResponse;
  }
}

2. 修复DemoApplication启动类

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

@SpringBootApplication
@ComponentScan(basePackages = "com.example.*")
public class DemoApplication extends SpringBootServletInitializer {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

3. 修复SwaggerConfig配置类

package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration  
public class SwaggerConfig {
  @Bean  
  public Docket api() {  
     return new Docket(DocumentationType.SWAGGER_2);  
  }
}

接口调用注意事项

使用Postman、Apipost等接口调试工具发起请求:

  • 请求方式:POST
  • 请求地址:http://localhost:8080/demo/newcall/newResp
  • 请求头添加:Content-Type: application/json
  • 请求体传入JSON格式的字符串参数即可

内容的提问来源于stack exchange,提问作者Srv654Java

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 13:45:04