org.springframework.boot Spring-Boot-Starter-Parent-Parent 2.7.13,
ContentCachingResponseWrapper,
deferredresult,
openjdk 1.8.0_202 < /p>
Фильтр: < /p>
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ContentCachingFilter extends OncePerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(ContentCachingFilter.class);
public static final String REST_UUID = "X-request-id";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
if (request.getHeader(REST_UUID) == null) {
String uuid = UUID.randomUUID().toString();
request.setAttribute(REST_UUID, uuid);
response.setHeader(REST_UUID, uuid);
} else {
String headerParam = request.getHeader(REST_UUID);
request.setAttribute(REST_UUID, headerParam);
response.setHeader(REST_UUID, headerParam);
}
logger.info("Before ContentCachingFilter");
filterChain.doFilter(request, responseWrapper);
logger.info("After ContentCachingFilter");
responseWrapper.copyBodyToResponse();
}
}
< /code>
Сервлет: < /p>
@Slf4j
@RestController
@RequestMapping("/servlet")
public class MyServlet {
int counter = 1;
@PostMapping("/deferred")
DeferredResult doDeferred(HttpServletRequest request, @RequestBody String requestBody) throws InterruptedException {
final ResponseEntity timeoutResponse = ResponseEntity.status(503).body("Deferred timeout");
final DeferredResult responseEntityDeferredResult = new DeferredResult(6000L,timeoutResponse);
if(requestBody == null){
requestBody = "anything";
}
final String finalRequestBody = requestBody;
counter++;
System.out.println(counter);
if(finalRequestBody.contains("sleep")){
ForkJoinPool.commonPool().submit(() -> {
log.info("Processing in separate thread");
if(finalRequestBody.equals("sleepMore")){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
} else {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
}
log.info("After the sleep execution");
responseEntityDeferredResult.setResult(ResponseEntity.status(200).body("Deferred successfully returned"));
log.info("Finished processing in separate thread");
});
} else {
responseEntityDeferredResult.setResult(ResponseEntity.status(200).body("Deferred successfully returned"));
}
return responseEntityDeferredResult;
}
}
< /code>
Результат службы: < /p>
C:\Users\XXX\Desktop\CURL>curl -H "Content-Type:text/plain" --trace-time http://localhost:18080/servlet/deferred -d
sleep -v
13:16:39.266200 * Host localhost:18080 was resolved.
13:16:39.275872 * IPv6: ::1
13:16:39.278151 * IPv4: 127.0.0.1
13:16:39.286604 * Trying [::1]:18080...
13:16:39.291555 * Connected to localhost (::1) port 18080
13:16:39.295087 > POST /servlet/deferred HTTP/1.1
13:16:39.295087 > Host: localhost:18080
13:16:39.295087 > User-Agent: curl/8.9.1
13:16:39.295087 > Accept: */*
13:16:39.295087 > Content-Type:text/plain
13:16:39.295087 > Content-Length: 5
13:16:39.295087 >
13:16:39.314630 * upload completely sent off: 5 bytes
13:16:43.443178 < HTTP/1.1 200
13:16:43.445868 < X-request-id: faec4178-3dc7-4e5f-a5bc-55d7ff46629a
13:16:43.449375 < Content-Type: text/plain;charset=UTF-8
13:16:43.452905 < Content-Length: 30
13:16:43.454768 < Date: Mon, 27 Jan 2025 12:16:44 GMT
13:16:43.458651 <
13:17:44.198648 * end of response with 30 bytes missing
13:17:44.203690 * closing connection #0
curl: (18) end of response with 30 bytes missing
< /code>
Ожидаемый результат службы при использовании объекта Httpservletresponse вместо объекта ContentCachingResponseWrapper: < /p>
C:\Users\XXX\Desktop\CURL>curl -H "Content-Type:text/plain" --trace-time http://localhost:18080/servlet/deferred -d sleep -v
13:32:24.414975 * Host localhost:18080 was resolved.
13:32:24.423820 * IPv6: ::1
13:32:24.426342 * IPv4: 127.0.0.1
13:32:24.434983 * Trying [::1]:18080...
13:32:24.439253 * Connected to localhost (::1) port 18080
13:32:24.443102 > POST /servlet/deferred HTTP/1.1
13:32:24.443102 > Host: localhost:18080
13:32:24.443102 > User-Agent: curl/8.9.1
13:32:24.443102 > Accept: */*
13:32:24.443102 > Content-Type:text/plain
13:32:24.443102 > Content-Length: 5
13:32:24.443102 >
13:32:24.462354 * upload completely sent off: 5 bytes
13:32:28.591708 < HTTP/1.1 200
13:32:28.594241 < X-request-id: a7d2adef-1155-4903-8b11-bc4836af7c32
13:32:28.597334 < Content-Type: text/plain;charset=UTF-8
13:32:28.600276 < Content-Length: 30
13:32:28.602554 < Date: Mon, 27 Jan 2025 12:32:28 GMT
13:32:28.605220 <
Deferred successfully returned13:32:28.606942 * Connection #0 to host localhost left intact
< /code>
httpservletresponse знает о ответе сервлета, ContentCachingResponseWrapper не является. Почему я не получаю полезную нагрузку ответа на обслуживание, используя ContentCachingResponseWrapper?
Подробнее здесь: https://stackoverflow.com/questions/793 ... the-client
ContentCachingResponseWrapper не возвращает ответ сервлета на клиент ⇐ JAVA
Программисты JAVA общаются здесь
1738048172
Anonymous
org.springframework.boot Spring-Boot-Starter-Parent-Parent 2.7.13,
ContentCachingResponseWrapper,
deferredresult,
openjdk 1.8.0_202 < /p>
Фильтр: < /p>
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ContentCachingFilter extends OncePerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(ContentCachingFilter.class);
public static final String REST_UUID = "X-request-id";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
if (request.getHeader(REST_UUID) == null) {
String uuid = UUID.randomUUID().toString();
request.setAttribute(REST_UUID, uuid);
response.setHeader(REST_UUID, uuid);
} else {
String headerParam = request.getHeader(REST_UUID);
request.setAttribute(REST_UUID, headerParam);
response.setHeader(REST_UUID, headerParam);
}
logger.info("Before ContentCachingFilter");
filterChain.doFilter(request, responseWrapper);
logger.info("After ContentCachingFilter");
responseWrapper.copyBodyToResponse();
}
}
< /code>
Сервлет: < /p>
@Slf4j
@RestController
@RequestMapping("/servlet")
public class MyServlet {
int counter = 1;
@PostMapping("/deferred")
DeferredResult doDeferred(HttpServletRequest request, @RequestBody String requestBody) throws InterruptedException {
final ResponseEntity timeoutResponse = ResponseEntity.status(503).body("Deferred timeout");
final DeferredResult responseEntityDeferredResult = new DeferredResult(6000L,timeoutResponse);
if(requestBody == null){
requestBody = "anything";
}
final String finalRequestBody = requestBody;
counter++;
System.out.println(counter);
if(finalRequestBody.contains("sleep")){
ForkJoinPool.commonPool().submit(() -> {
log.info("Processing in separate thread");
if(finalRequestBody.equals("sleepMore")){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
} else {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
}
log.info("After the sleep execution");
responseEntityDeferredResult.setResult(ResponseEntity.status(200).body("Deferred successfully returned"));
log.info("Finished processing in separate thread");
});
} else {
responseEntityDeferredResult.setResult(ResponseEntity.status(200).body("Deferred successfully returned"));
}
return responseEntityDeferredResult;
}
}
< /code>
Результат службы: < /p>
C:\Users\XXX\Desktop\CURL>curl -H "Content-Type:text/plain" --trace-time http://localhost:18080/servlet/deferred -d
sleep -v
13:16:39.266200 * Host localhost:18080 was resolved.
13:16:39.275872 * IPv6: ::1
13:16:39.278151 * IPv4: 127.0.0.1
13:16:39.286604 * Trying [::1]:18080...
13:16:39.291555 * Connected to localhost (::1) port 18080
13:16:39.295087 > POST /servlet/deferred HTTP/1.1
13:16:39.295087 > Host: localhost:18080
13:16:39.295087 > User-Agent: curl/8.9.1
13:16:39.295087 > Accept: */*
13:16:39.295087 > Content-Type:text/plain
13:16:39.295087 > Content-Length: 5
13:16:39.295087 >
13:16:39.314630 * upload completely sent off: 5 bytes
13:16:43.443178 < HTTP/1.1 200
13:16:43.445868 < X-request-id: faec4178-3dc7-4e5f-a5bc-55d7ff46629a
13:16:43.449375 < Content-Type: text/plain;charset=UTF-8
13:16:43.452905 < Content-Length: 30
13:16:43.454768 < Date: Mon, 27 Jan 2025 12:16:44 GMT
13:16:43.458651 <
13:17:44.198648 * end of response with 30 bytes missing
13:17:44.203690 * closing connection #0
curl: (18) end of response with 30 bytes missing
< /code>
Ожидаемый результат службы при использовании объекта Httpservletresponse вместо объекта ContentCachingResponseWrapper: < /p>
C:\Users\XXX\Desktop\CURL>curl -H "Content-Type:text/plain" --trace-time http://localhost:18080/servlet/deferred -d sleep -v
13:32:24.414975 * Host localhost:18080 was resolved.
13:32:24.423820 * IPv6: ::1
13:32:24.426342 * IPv4: 127.0.0.1
13:32:24.434983 * Trying [::1]:18080...
13:32:24.439253 * Connected to localhost (::1) port 18080
13:32:24.443102 > POST /servlet/deferred HTTP/1.1
13:32:24.443102 > Host: localhost:18080
13:32:24.443102 > User-Agent: curl/8.9.1
13:32:24.443102 > Accept: */*
13:32:24.443102 > Content-Type:text/plain
13:32:24.443102 > Content-Length: 5
13:32:24.443102 >
13:32:24.462354 * upload completely sent off: 5 bytes
13:32:28.591708 < HTTP/1.1 200
13:32:28.594241 < X-request-id: a7d2adef-1155-4903-8b11-bc4836af7c32
13:32:28.597334 < Content-Type: text/plain;charset=UTF-8
13:32:28.600276 < Content-Length: 30
13:32:28.602554 < Date: Mon, 27 Jan 2025 12:32:28 GMT
13:32:28.605220 <
Deferred successfully returned13:32:28.606942 * Connection #0 to host localhost left intact
< /code>
httpservletresponse знает о ответе сервлета, ContentCachingResponseWrapper не является. Почему я не получаю полезную нагрузку ответа на обслуживание, используя ContentCachingResponseWrapper?
Подробнее здесь: [url]https://stackoverflow.com/questions/79392996/contentcachingresponsewrapper-is-not-returning-servlet-response-to-the-client[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия