| | |
| | | import io.netty.handler.codec.http.HttpResponseStatus; |
| | | import io.netty.util.CharsetUtil; |
| | | |
| | | import java.util.Map; |
| | | |
| | | import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE; |
| | | import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| | | |
| | |
| | | |
| | | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| | | } |
| | | |
| | | public static void sendResponseJson2(ChannelHandlerContext ctx, HttpResponseStatus status, |
| | | String message,Map<String, Object> map) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("code", status.code()); |
| | | jsonObject.put("msg", message); |
| | | jsonObject.put("data", map); |
| | | |
| | | FullHttpResponse response = new DefaultFullHttpResponse( |
| | | HTTP_1_1, status, Unpooled.copiedBuffer(jsonObject.toJSONString(), CharsetUtil.UTF_8)); |
| | | response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8"); |
| | | |
| | | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| | | } |
| | | } |