[class misc.contract.response.Response] 没有带有预设 Content-Type 'text/event-stream;charset=UTF-8' 的转换器

问题描述 投票:0回答:1

当我从用 spring boot 编写的 java 11 服务器端返回 sse 消息时

springBootVersion: '2.6.6',
,显示如下错误:

No converter for [class misc.contract.response.Response] with preset Content-Type 'text/event-stream;charset=UTF-8'

这是服务器端 api 定义:

package com.dolphin.soa.post.controller.user.ai;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
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.servlet.mvc.method.annotation.SseEmitter;

/**
 * @version 1.0
 * @author: dolphin
 * @date: 2023-03-18 12:50
 */

@RequestMapping("/ai/stream/chat")
@FeignClient(name = "dolphin-post-service")
public interface IAiSseChatController {
    /**
     *
     * @return
     */
    @GetMapping(path="/ask",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    SseEmitter askStream(@RequestParam(required = true) String question);

}

我应该怎么做才能解决这个问题?服务器端默认返回json,服务器端发送的事件响应是最近新增的。这是

Response
定义:

package misc.contract.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import misc.exception.ServiceException;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Data
@Schema
public class Response<T> {

    public static final String MSG_OK       = "ok";

    private String resultCode = ServiceException.API_OK;

    private String statusCode = ServiceException.API_OK;

    private String msg = MSG_OK;

    private T result;

    private Map<String, List<String>> validationErrors;

    public Response(ServiceException e, Map<String, List<String>> validationErrors) {
        this(e);
        this.validationErrors = validationErrors;
    }

    public Response(ServiceException e) {
        this(e.getMessage(), e.getStatusCode(), e.getResultCode());
    }

    public Response(String msg, String statusCode, String resultCode) {
        this.msg = msg;
        this.statusCode = statusCode;
        this.resultCode = resultCode;
    }

    public Response(T result)
    {
        this.result = result;
    }

    public Response() {
    }
}
spring-boot java-11 server-sent-events
1个回答
0
投票

我有一个类似的问题,我按照这个 Baeldung 教程解决了。结果是这样的

@Configuration
public class EventStreamConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters (List<HttpMessageConverter<?>> converters) {
        converters.add(new EventStreamHttpMessageConverter());
    }

}

转换器在哪里

public class EventStreamHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {

    public EventStreamHttpMessageConverter() {
        super(new MediaType("text", "event-stream"));
    }

    //implementation of the abstract methods...
}
© www.soinside.com 2019 - 2024. All rights reserved.