-
Notifications
You must be signed in to change notification settings - Fork 61
Handle htmx headers for @ResponseBody methods #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponseBodyAdvice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.mvc; | ||
|
|
||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.converter.HttpMessageConverter; | ||
| import org.springframework.http.server.ServerHttpRequest; | ||
| import org.springframework.http.server.ServerHttpResponse; | ||
| import org.springframework.http.server.ServletServerHttpRequest; | ||
| import org.springframework.http.server.ServletServerHttpResponse; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
|
||
| /** | ||
| * A {@link ResponseBodyAdvice} implementation that adds htmx response headers | ||
| * based on {@link HtmxResponse}. | ||
| * | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HtmxResponseBodyAdvice implements ResponseBodyAdvice<Object> { | ||
|
|
||
| private final HtmxHandlerMethodHandler htmxHandlerMethodHandler; | ||
|
|
||
| public HtmxResponseBodyAdvice(HtmxHandlerMethodHandler htmxHandlerMethodHandler) { | ||
| this.htmxHandlerMethodHandler = htmxHandlerMethodHandler; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
| return true; // Apply to all @ResponseBody methods | ||
| } | ||
|
|
||
| @Override | ||
| public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, | ||
| Class<? extends HttpMessageConverter<?>> selectedConverterType, | ||
| ServerHttpRequest request, ServerHttpResponse response) { | ||
|
|
||
| if (request instanceof ServletServerHttpRequest servletRequest | ||
| && response instanceof ServletServerHttpResponse servletResponse) { | ||
| htmxHandlerMethodHandler.handleMethodArgument(servletRequest.getServletRequest(), | ||
| servletResponse.getServletResponse()); | ||
| } | ||
| return body; | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
...ot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponseBodyAdviceIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.mvc; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.test.web.servlet.client.RestTestClient; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
| @Import({ HtmxResponseBodyAdviceIT.TestRestController.class }) | ||
| @AutoConfigureRestTestClient | ||
| public class HtmxResponseBodyAdviceIT { | ||
|
|
||
| @Autowired | ||
| RestTestClient webClient; | ||
|
|
||
| @Test | ||
| public void testTrigger() throws Exception { | ||
|
|
||
| get("/trigger") | ||
| .expectHeader() | ||
| .valueEquals("HX-Trigger", "trigger1,trigger2"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionHandler() throws Exception { | ||
|
|
||
| get("/throw-exception") | ||
| .expectHeader() | ||
| .valueEquals("HX-Retarget", "#container"); | ||
| } | ||
|
|
||
| private RestTestClient.ResponseSpec get(String uri) { | ||
|
|
||
| return webClient | ||
| .get() | ||
| .uri(uri) | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk(); | ||
| } | ||
|
|
||
| @RestController | ||
| static class TestRestController { | ||
|
|
||
| @GetMapping("/trigger") | ||
| public Object triggerResponseBody(HtmxResponse response) { | ||
|
|
||
| response.addTrigger("trigger1"); | ||
| response.addTrigger("trigger2"); | ||
| return "response"; | ||
| } | ||
|
|
||
| @GetMapping("/throw-exception") | ||
| public Object throwException() { | ||
|
|
||
| throw new RuntimeException(); | ||
| } | ||
|
|
||
| @ExceptionHandler(RuntimeException.class) | ||
| public Object handleError(RuntimeException ex, HtmxResponse htmxResponse) { | ||
|
|
||
| htmxResponse.setRetarget("#container"); | ||
| return "view"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add back the
getRequestMappingHandlerMappingmethod? Otherwise, the request mapping is broken.