As part of SNS optimization we have to change our query parameters to path parameters, so:
/somesite?hl=ja
Has to become:
/somesite/hl/ja
How do we do that without adding a separate path to every single #RequestMapping method? It doesn't look like I can do it with an Interceptor. We are using Spring 4
You can try adding filter instead of interceptor to rewrite the request URI.
If you are free to use any third-party library, you can use this http://tuckey.org/urlrewrite/.
Or you can write your own impl in filter method.
Follow this question for implementation. servlet filter to rewrite URL
Related
What is the best way to change path parameter (may be using interceptor) before passing it to controllers ? I am using spring mvc.
Right now, i am adding an extra parameter to get the job done, and checking for that in the controller, but i would rather change the parameter and not change anything in the controller
I am collecting custom metrics for my controller endpoints via HandlerInterceptor. In postHandle I have everything I need and I would like to save the metrics for my endpoints along with the original route defined in the controller, so not the actual route filled with the data.
E.g. I have #GetMapping("/cats/{name}") and make GET request on /cats/tom I still want the defined route "cats/{name}"
I found that I can do this with the Object handler which is the parameter of postHandle -> I can cast it to HandlerMethod get the annotations of the method and find it from memberValues. This seems a bit overkill I also have to filter for Get-, Post-, Put-, Delete-, Patch-, RequestMapping annotations. Is there any easier or more straightforward way to do this? If so how? If not what else do I have to take into consideration with this solution?
It turns out Spring adds this data to the HttpServletRequest attributes. So it can be retrieved using:
String routePattern = (String) request.getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern");
I am using Jersey to implement rest api and Jackson to provide JSON support. I am trying to remove certain properties before serialization by overriding BeanSerializerModifier.changeProperties method.
But removing properties will be based on query parameter. Is there any way to access the query parameter in my implementation?
Use of BeanSerializerModifier itself would get complicated as the method is only called once when construction necessarily JsonSerializer for the first time. As to passing query parameters, you could pass them using contextual attributes and ObjectWriter (constructed from ObjectMapper), but that means taking over quite a bit of serialization automation from Jersey.
There is one mechanism that could be helpful in modifying serialization aspects without taking over the whole process: registering ObjectWriterModifier, using ObjectWriterInjector. These are part of Jackson JAX-RS provider, added in Jackson 2.3. Without knowing more details I don't know how easy this would be; part of the issue is that query parameters are more of an input side things, so there is no direct access to them from output processing side.
I try to use Spring integration as a simple mediator. We have a webapp residing on the intranet which can not be accessed from the outside. Now I want to expose this app through a mediator in the DMZ. This simply means that all url:s like http://gateway/TheApp/* should be forwarded to http://internal/TheApp/*. Really simple in theory but it seems that the component for handling outgoing http-reuests in Spring integration (HttpRequestExecutingMessageHandler) needs an explicut URI, i.e no wildcard matching. Can this be achieved with Spring integration?
The inbound gateway puts the url into a header; you can simply use an expression on the outbound gateway...
url-expression="headers['http_requestUrl'].replace('gateway', 'internal')"
If you are using Java #Configuration, you can use the constructor that takes an Expression...
Expression expression = new SpelExpressionParser().parseExpression(
"headers['http_requestUrl'].replace('gateway', 'internal')");
This test case does something similar by grabbing just the query string and appends it to a new URL.
In that case, it puts the query string in the payload; it could easily by added as a header instead (e.g. for a POST), using the <int:header/> child element.
I'm wanting to to add an endpoint like /user/foo where foo is one of a set of values determined at runtime. I'm wondering what the best way is to do this in Spring, or indeed if it should even been done in Spring an not handled at the controller level.
I'm currently using Springs security filter chain, so I did think about putting a filter in front of /user/* to do this validation. Is this a reasonable solution or is there a more desirable solution I have missed?
You can use #PathVariable annotation on a method argument. #PathVariable also allows regex if you need to validate the structure of the varible.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/PathVariable.html
and for the regex
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html