springboot-返回图片数据

@ApiOperation(value = "生成日程分享二维码")
@RequestMapping(value = "/pc/r/eventhandle/sharepic", method = RequestMethod.POST )

@ResponseBody

@ApiImplicitParams({@ApiImplicitParam(name = "eventId", value = "日程id", paramType = "query", required = true)
})
public Result.Base getEventSharePic(Long eventId, HttpServletRequest request, HttpServletResponse response) {
    User user = getRequestUser(request);
    Result.Base result = iEventHandleService.getEventSharePic(eventId, user.getId(), response);
    return result;
}

 

// 生成日程分享二维码
@Override
public Result.Base getEventSharePic(Long eventId, Long userId, HttpServletResponse response){
    // 通过userid和eventid获取usercalendar,没有则说明没权限
    UserCalendar userCalendar = iUserCalendarDao.getUserCalendarByEventId(userId, eventId);
    if (userCalendar == null || userCalendar.getRights() > CalendarRightEnum.EDIT.getValue()) {
        logger.error("getEventSharePic error :{}, userId:{}, eventId{}",
                ErrorCode.NO_RIGHT_ERROR, userId, eventId);
        return ErrorCode.NO_RIGHT_ERROR;
    }

    String shareUrl = eventShareMiniUrl + "?id=" + eventId;
    try {
        OutputStream out = response.getOutputStream();
        response.setHeader("Content-Type","image/jpeg");
        QrCodeCreateUtil.createQrCode(out, shareUrl,90, "JPEG");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return new Result.Success<>();
}

运行在swaggerui中可以访问,并显示二维码图片,但是后台报错:

 

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:287)

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)

at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

 

解决办法:

需要将上面的@ResponseBody去掉。

如果类上使用的是@RestController

需要改为@Controller

因为ResponseBody相当于produces="application/json"

这里不写,或者写为:produces="image/jpeg",但是如果后面业务上没有给response赋值图片数据,会报错。

即:response赋值图片数据时这里才能写produces="image/jpeg"。因此不写就好了。

你可能感兴趣的:(java_springboot)