How would I escape HTML in SpringBoot's #PathVariable? - spring

I am using Spring 4.2.5 and I just want to escape the HTML in the URL if present. Using JSON deserializer is affecting the values of RequestBody/Param as well, but I don't want other values to be changed. What is the correct way to do this?

Apache's URL builder escapes html in parameters automatically:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html
If you're talking about html in the spring Rest controller, a filter or Jackson configuration should solve your problem.

I guess I need to use PropertyEditorSupport class to escape the HTML in the PathVariable and implement JSON deserializer for the RequestBody.

Related

Get Jackson ObjectMapper in Quarkus

I am writing a custom OpenApiConfigurator that adds some examples to my api dynamically.
When I add the examples using the value field of io.smallrye.openapi.api.models.examples.ExampleImpl, which is an object, the example is null in swagger-ui. It only works when I added the actual json.
To add the actual json I have to generate it from my response dto using Jackson. But how can I access the quarkus object mapper, for which I have some customisations using ObjectMapperCustomizer, if in the OpenApiConfigurator CDI is not available?
It's actually possible to access the CDI container statically with Arc.container().instance(ObjectMapper::class.java).get()
That solved it for me.

How can I ingore placeholders in property file in spring boot?

I have urls like localhost:8080/boot/${task}/say
in property file, I am reading this url in my code using poprerty placeholder, problem is here spring boot trying to search placeholder in url. I want to ingore that and send complete url with that placeholder into response.
I have already declared bean of PropertySourcePlaceholderConfigure and setIgoreUnresolvablePlaceholders as true but still it asking for value of placeholder.
You need to escape $ sign.
localhost:8080/boot/#{'$'}{task}/say

Possible to use #RepositoryRestResource with #PathVariable instead of #Param?

I have a RestRepositoryResource which is working properly. However, I would prefer to structure URLs using path variables instead of query parameters. The goal would be this:
http://localhost/persons/findByLastName/Smith/
instead of this:
http://localhost/persons/findByLastName?lastName=Smith
I have played around with various annotations but not achieved this using RestRepositoryResource. Is this possible or does this have to be done with a Controller resource mapping?
Spring data repositories don't support the #PathVariable annotation right now. However a simple workaround for this problem might be to use URLRewriteFilter internally re-route the request for http://localhost/persons/findByLastName/Smith/ to http://localhost/persons/findByLastName?lastName=Smith without the user noticing.

Enunciate validation of Jersey API fails with #POST #FormParam + String data

I'm evaluating enunciate to document our REST APIs and I'm having an issue with the validation step:
Validation result has errors.
my.java: error: [core] An entity parameter must be of type MultivaluedMap<String, String> if there is another parameter annotated with #FormParam.
#FormParam("my-param") String myParam, String data)
This construct of accepting the POST data as a String entity in addition to #FormParam bindings is supported by Jersey, so not sure why enunciate is choking on it? Is this not JAX-RS compliant?
This is a really useful to capture the full post data for auditing purposes if something went wrong. Is there a way to configure enunciate to ignore this argument?
If not, is there some other way to capture the post data in a way that would keep enunciate happy? I'm reluctant to go to MultivaluedMap as the stringification process may not result in exactly the String which was passed in...
Thanks!
It may be that the validation checks Enunciate performs are outdated. You might consider requesting a change by submitting a JIRA issue.
One thing you could try as a workaround is to create your own custom parameter annotation and configure Enunciate consider it as a custom resource parameter.
<enunciate>
...
<services>
<rest>
<custom-resource-parameter-annotation qualifiedName="org.myco.CustomResourceParam"/>
</rest>
</services>
...
</enunciate>
However, if the purpose of the parameter is for auditing, I'd really recommend using a Jersey filter. That way you don't litter your API code with auditing concerns.

Hibernate Validator Not Correctly Displaying Unicode Characters

I am building a Spring MVC web application that uses JSR-303 validation. With the first form I created, I added a couple of validation annotations (including custom error message codes) to the form backing bean. I also created ValidationMessages.properties and ValidationMessages_en.properties files.
Everything seems to be working correctly with one exception: multi-byte utf-8 encoded characters are not displayed correctly (e.g., "ñ" is displayed as "ñ").
This is not a problem with my standard messages.properties and messages_en.properties files that I use for field labels and other text, so I'm assuming it's an issue with the hibernate validator code. Has anyone else had this issue and solved it? FYI, I'm using Hibernate version 4.3.0.Final.
Thanks,
Peter
In my properties files I must include special characters like this:
\u00F3 instead of ó
In that way they are shown well.
Hope it helps.
P.S.: Using ResourceBundleEditor from Eclipse also helps.

Resources