Source:
<code>public ResponseEntity<String> serveEndpoint( @PathVariable("endpoint") String endpointName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body, HttpServletRequest request) { getLogger().debug("Endpoint: {}, method: {}, request body: {}", endpointName, methodName, body); VaadinEndpointData vaadinEndpointData = vaadinEndpoints .get(endpointName.toLowerCase(Locale.ENGLISH)); if (vaadinEndpointData == null) { getLogger().debug("Endpoint '{}' not found", endpointName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinEndpointData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in endpoint '{}'", methodName, endpointName); return ResponseEntity.notFound().build(); } try { VaadinServletService service = (VaadinServletService)VaadinService.getCurrent(); <START> CurrentInstance.set(VaadinRequest.class, new VaadinServletRequest(request, service)); <END> return invokeVaadinEndpointMethod(endpointName, methodName, methodToInvoke, body, vaadinEndpointData, request); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize endpoint '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", endpointName, methodName, VAADIN_ENDPOINT_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } }</code><technical_language>clear instance in a finally block avoid memory leaks</technical_language>

Target:
public ResponseEntity<String> serveEndpoint( @PathVariable("endpoint") String endpointName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body, HttpServletRequest request) { getLogger().debug("Endpoint: {}, method: {}, request body: {}", endpointName, methodName, body); VaadinEndpointData vaadinEndpointData = vaadinEndpoints .get(endpointName.toLowerCase(Locale.ENGLISH)); if (vaadinEndpointData == null) { getLogger().debug("Endpoint '{}' not found", endpointName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinEndpointData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in endpoint '{}'", methodName, endpointName); return ResponseEntity.notFound().build(); } try { VaadinServletService service = (VaadinServletService)VaadinService.getCurrent(); CurrentInstance.set(VaadinRequest.class, new VaadinServletRequest(request, service)); return invokeVaadinEndpointMethod(endpointName, methodName, methodToInvoke, body, vaadinEndpointData, request); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize endpoint '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", endpointName, methodName, VAADIN_ENDPOINT_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } finally { CurrentInstance.set(VaadinRequest.class, null); } }