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

SpringBoot控制器已显式设置Content-Type为JSON却返回XML

刚好有过类似的实现经验,我来帮你把这个Spring Boot生产者-消费者REST服务的完整流程梳理清楚,结合你给出的生产者POST端点代码,补充完整示例和关键注意点:

Spring Boot生产者-消费者REST服务实现(RestTemplate调用示例)

1. 生产者服务:JSON内容POST端点实现

首先完善你给出的生产者控制器代码,包含请求参数接收、业务逻辑模拟和响应封装:

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class CarProducerController {

    @RequestMapping(value = "/cars", method = RequestMethod.POST)
    public ResponseEntity<Cars> getCars(@RequestBody CarRequest requestParam) {
        // 模拟业务逻辑:根据请求参数生成Cars数据
        Cars cars = new Cars();
        cars.setCarList(List.of(
                new Car("Tesla", "Model 3"),
                new Car("Ford", "Mustang")
        ));

        // 构建响应头,明确指定返回JSON格式
        HttpHeaders respHeader = new HttpHeaders();
        respHeader.set("Content-Type", "application/json");

        // 返回包含响应体、头和状态码的ResponseEntity
        return new ResponseEntity<>(cars, respHeader, HttpStatus.OK);
    }

    // 辅助实体类(需确保与消费者端结构一致)
    static class CarRequest {
        private String type;
        // getter/setter
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
    }

    static class Cars {
        private List<Car> carList;
        // getter/setter
        public List<Car> getCarList() { return carList; }
        public void setCarList(List<Car> carList) { this.carList = carList; }
    }

    static class Car {
        private String brand;
        private String model;
        // 构造器+getter/setter
        public Car(String brand, String model) {
            this.brand = brand;
            this.model = model;
        }
        public String getBrand() { return brand; }
        public void setBrand(String brand) { this.brand = brand; }
        public String getModel() { return model; }
        public void setModel(String model) { this.model = model; }
    }
}

2. 消费者服务:用RestTemplate调用生产者端点

2.1 先配置RestTemplate Bean

在消费者服务中注入RestTemplate之前,需要先配置Bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2.2 消费者服务调用逻辑

编写服务类,通过RestTemplate调用生产者的POST端点:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@Service
public class CarConsumerService {

    private final RestTemplate restTemplate;

    // 构造器注入RestTemplate(推荐方式)
    public CarConsumerService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public Cars fetchCarsFromProducer() {
        // 生产者服务的访问地址(生产环境建议通过配置文件管理)
        String producerUrl = "http://localhost:8080/cars";

        // 构建请求头,指定发送的内容类型为JSON
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");

        // 构造请求参数(与生产者的CarRequest结构一致)
        CarRequest request = new CarRequest();
        request.setType("electric");

        // 封装请求实体(请求头+请求体)
        HttpEntity<CarRequest> requestEntity = new HttpEntity<>(request, headers);

        // 调用生产者POST端点,指定响应类型为Cars
        ResponseEntity<Cars> responseEntity = restTemplate.exchange(
                producerUrl,
                HttpMethod.POST,
                requestEntity,
                Cars.class
        );

        // 返回响应体内容
        return responseEntity.getBody();
    }

    // 与生产者一致的实体类
    static class CarRequest {
        private String type;
        // getter/setter
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
    }

    static class Cars {
        private List<Car> carList;
        // getter/setter
        public List<Car> getCarList() { return carList; }
        public void setCarList(List<Car> carList) { this.carList = carList; }
    }

    static class Car {
        private String brand;
        private String model;
        // getter/setter
        public String getBrand() { return brand; }
        public void setBrand(String brand) { this.brand = brand; }
        public String getModel() { return model; }
        public void setModel(String model) { this.model = model; }
    }
}

3. 关键注意事项

  • 地址与端口:确保消费者调用的生产者URL与实际部署地址一致,生产环境建议用配置文件或配置中心管理地址。
  • 实体类一致性:生产者和消费者的实体类字段需保持一致,可通过@JsonProperty指定字段别名解决字段名差异问题。
  • 异常处理:生产环境中要为RestTemplate添加异常捕获逻辑,比如处理HttpClientErrorException(客户端错误)、HttpServerErrorException(服务端错误),避免调用失败导致程序崩溃。
  • 请求头匹配:如果生产者要求特定请求头(如认证Token),消费者需在请求中添加对应头信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 07:05:12