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

使用Postman请求接口返回401 Unauthorized问题求助

问题分析与解决方案

核心问题1:请求路径不匹配

你请求的接口是http://localhost:9690/regime/add,但PatientController类上标注的@RequestMapping(path ="/patient"),内部新增接口的映射是@PostMapping("/add"),正确的新增接口路径应为:
http://localhost:9690/patient/add

核心问题2:401 Unauthorized 排查方向

若修正路径后仍返回401,需排查以下内容:

  • 项目是否引入Spring Security等权限依赖?如果有,需配置对应接口的权限规则,或在开发环境临时关闭不必要的认证校验。
  • 请求头是否遗漏认证信息(如Token)?若项目启用Token认证,需在Postman的请求头中添加Authorization字段并填入对应凭证。

额外配置错误(影响数据库运行,与401无关)

application.properties中的Hibernate方言配置错误:

spring.jpa.hibernate.dialect=org.hibernate.dialect.nutridb

需改为对应MySQL版本的方言,例如MySQL 8.x版本使用:

spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

控制器代码确认

你的PatientController新增接口映射逻辑正确,修正请求路径后,无权限限制的情况下即可正常访问:

@AllArgsConstructor 
@RestController 
@RequestMapping(path ="/patient") 
public class PatientController
{
    @Autowired
    private PatientService patientService;
    
    @GetMapping("/get")
    public List<Patient> getAll(){
        return patientService.getPatients();
    }
    
    @GetMapping("/getid/{patientId}")
    public Optional<Patient> getByID(@PathVariable("patientId") Long patientId){
        return patientService.getPatient(patientId);
    }
    
    @PostMapping("/add")
    public Patient save(@RequestBody Patient patient)  {
        return patientService.save(patient);
    }
    
    @DeleteMapping("/delete/{patientId}")
    public void delete(@PathVariable("patientId") Long patientId){
        patientService.delete(patientId);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 23:31:06