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

Spring Boot创建治疗师接口Jackson反序列化错误排查

治疗师创建接口415错误及Jackson反序列化问题

接口实现代码

@Transactional
@PostMapping("/crear")
public Terapeuta crearTerapeuta(@RequestBody TerapeutaRequest terapeutaRequest) {
    // 此处可访问治疗师和专业方向数据
    Terapeuta terapeuta = terapeutaRequest.getTerapeuta();
    Long especialidadId = terapeutaRequest.getEspecialidadId();
    
    // 添加创建治疗师并关联专业方向的逻辑
    return terapeutaService.crearTerapeutaConEspecialidad(terapeuta, especialidadId);
}

Postman请求参数

请求地址:http://localhost:8080/terapeuta/crear
请求体(已设置Content-Type为application/json):

{
    "terapeuta": {
        "nombre": "John",
        "apellido": "Doe",
        "email": "johndoe@example.com",
        "phone": "1234567890",
        "password": "securepassword",
        "license": "ABCD1234"
    },
    "especialidadId": 1
}

错误响应(415 Unsupported Media Type)

{
    "timestamp": "2024-06-08T12:27:02.154+00:00",
    "status": 415,
    "error": "Unsupported Media Type",
    "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported\r\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:213)\r\n\tat
ETC...
ETC...
org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)\r\n\tat java.base/java.lang.Thread.run(Thread.java:840)\r\n",
    "message": "Content-Type 'application/json;charset=UTF-8' is not supported.",
    "path": "/terapeuta/crear"
}

控制台关键日志

m.c.j.MappingJackson2HttpMessageConverter Failed to evaluate Jackson deserialization for type [[simple type, class com.example.demo.model.TerapeutaRequest]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'terapeutaPacientesAtendidos': no back reference property found from type `java.util.List<com.example.demo.model.TerapeutaPaciente>`
GestionTerapias m.w.s.m.s.DefaultHandlerExceptionResolver Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported]

已尝试的操作

  • 修改@JsonManagedReference注解配置
  • 删除@JsonManagedReference注解
  • 调整实体类映射关系
  • 使用@JsonIdentityInfo注解
    上述操作后仍出现415错误或500 StackOverflowError

解决方案

修复双向引用注解匹配问题

日志明确指出terapeutaPacientesAtendidos管理引用找不到对应反向引用,需确保双向关联的注解名称完全匹配:

  1. 在Terapeuta类的关联字段上指定引用名称:
@OneToMany(mappedBy = "terapeuta", cascade = CascadeType.ALL)
@JsonManagedReference(value = "terapeuta-pacientes")
private List<TerapeutaPaciente> terapeutaPacientesAtendidos;
  1. 在TerapeutaPaciente类对应的Terapeuta字段上标注同名反向引用:
@ManyToOne
@JoinColumn(name = "terapeuta_id")
@JsonBackReference(value = "terapeuta-pacientes")
private Terapeuta terapeuta;

直接忽略关联字段序列化

如果创建治疗师接口不需要处理terapeutaPacientesAtendidos关联数据,直接用@JsonIgnore忽略该字段即可:

@OneToMany(mappedBy = "terapeuta", cascade = CascadeType.ALL)
@JsonIgnore
private List<TerapeutaPaciente> terapeutaPacientesAtendidos;

优化请求DTO设计

建议在TerapeutaRequest中使用独立的请求字段(而非直接嵌套Terapeuta实体),避免实体类关联关系干扰反序列化:

public class TerapeutaRequest {
    private String nombre;
    private String apellido;
    private String email;
    private String phone;
    private String password;
    private String license;
    private Long especialidadId;
    
    // getter和setter方法
}

之后在控制器中从请求DTO构造Terapeuta实体,减少实体关联带来的序列化问题。


内容的提问来源于stack exchange,提问作者Lucero Garay Pacheco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 17:25:55