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

SpringBoot创建微服务时遇HttpMessageNotWritableException错误求助

解决SpringBoot接口返回"No converter for Product with preset Content-Type 'null'"错误

问题场景

使用SpringBoot开发Product微服务,编写了Product实体类和ProductController控制器。通过Postman发送GET请求localhost:8081/products/details?user_Id=1&product_Id=1时,触发500内部服务器错误,错误提示为:No converter for [class com.scalermasterclass.productservice.models.Product] with preset Content-Type 'null'。

错误原因

  1. 响应类型不匹配:控制器的@RequestMapping指定了produces=MediaType.TEXT_PLAIN_VALUE,要求接口返回纯文本格式,但方法返回的是Product对象,Spring无法找到将对象转换为纯文本的消息转换器。
  2. 实体类序列化问题:Product类的属性都是私有且没有提供getter方法,即使将响应类型改为JSON,Jackson序列化工具也无法访问私有属性完成对象转JSON的操作。

解决方案

1. 修正控制器的响应类型

REST接口默认返回JSON格式,可直接移除produces指定,让Spring自动处理;也可以显式指定为JSON类型:

package com.scalermasterclass.productservice.controllers;

import com.scalermasterclass.productservice.models.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path="/products") 
// 或者显式指定:@RequestMapping(path="/products", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
public class ProductController {
    @GetMapping("/details")
    public Product getProductDetails(@RequestParam("user_Id") Long userId,@RequestParam("product_Id") Long productId){
        Product product = new Product();
        // 给对象赋值测试
        product.setId(1L);
        product.setTitle("测试商品");
        product.setDescription("测试商品描述");
        product.setCategory("测试分类");
        product.setPrice(99.9);
        product.setImageUrl("http://example.com/test.jpg");
        product.setPublic(true);
        return product;
    }
}

2. 给Product实体类添加访问方法

为私有属性添加getter/setter方法,确保Jackson能正常序列化对象:

package com.scalermasterclass.productservice.models;

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String description;
    private String category;
    private double price;
    private String imageUrl;
    private boolean isPublic;

    // Getter和Setter方法
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }

    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    public String getImageUrl() { return imageUrl; }
    public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; }

    public boolean isPublic() { return isPublic; }
    public void setPublic(boolean aPublic) { isPublic = aPublic; }
}

3. 可选:用Lombok简化代码

如果不想手动编写getter/setter,可以使用Lombok的@Data注解自动生成这些方法,需要先添加Lombok依赖:

  • Maven(pom.xml):
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
  • Gradle(build.gradle):
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

修改后的Product类:

package com.scalermasterclass.productservice.models;

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

@Data
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String description;
    private String category;
    private double price;
    private String imageUrl;
    private boolean isPublic;
}

验证

重启SpringBoot应用,再次发送GET请求,接口将正常返回JSON格式的Product数据,错误消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 15:20:05