Jersey Rest Client with empty string param - jersey

I'm using Jersey to consume some rest services, but I having a problem when I try to sent a parameter empty because seems that Jersey doesn't map the empty or null parameter.
Jersey generate something like (value is the empty string, the equals is lost and I get a 500 error):
http://myservice.test.com/services/rest/setAttribute?id=555&value&test=test
If I go to the browser an enter (this one works!!!):
http://myservice.test.com/services/rest/setAttribute?id=555&value=&test=test
The value field is required so I need to sent it every request, but I cannot set it in empty and sometimes I need the value an empty string.
Thanks...

You can add #JsonWriteNullProperties to the class you serializes to force it to write null values.

Related

How to handle invalid/extra special characters & = in request url-SpringBoot?

I have a Rest service where get call if I send multiple invalid/extra & and = characters then also my endpoint does not throw any error. I would like to throw back invalid request error if url contains any extra special character like & or =.
for example:
http://localhost:8080/myservice?rollNo=03456789321&school=Myschool //This is Okay for me
http://localhost:8080/myservice?rollNo=03456789321&school= //should throw error as school is not having value
http://localhost:8080/myservice?rollNo=03456789321&&&&school=Myschool
//should throw error as &&&& is multiple where it should only one
http://localhost:8080/myservice?rollNo=03456789321&= //should throw error as &= is there at end having no sence.
Note that , I am hitting these request from postman , and I have doubt that postman do something with these parameters, cause I am not able to find these extra characters in spring boot while debugging.
Any way through which i can get whole request url in my controller so that I can find out for these charecters comming?
Any built in springboot annotation is there to handle such a cases?
I got my problem solved.
After lot of research , and some observation I came to know that when you pass any number of characters among & and = in request url, the rest client tools like postman , or advanced rest client will refine the url before hitting actual server and remove those extra un-necessary characters. SO if you write multiple &&&& or == charecters in url , it will consider each extra & as blank parameter and will ignore while sending final request, only of those characters which has parameter names besides it it will taken as part of refined request.
you can see in screenshot bellow:
You can Use #RequestParam in your Spring Boot rest Controller
Something of the following
#GetMapping(value = "/myservice")
public boolean doSomething(#RequestParam("rollNo") Integer rollNo , #RequestParam("school") String school) {
doValidation(rollNo,school);
// Do Something
return true;
}
#RequestParam will make sure that your Url need to have these Params rollNo & school. Without it it will throw error.
But if you were to pass an empty string like &school= in your second example. The controller will get an empty String.
You can add a basic validation layer right before you do anything in you controller to handle this condition.

#RequestHeader required property behavior for request paramter and value

Can we make a header parameter mandatory but not the value using #RequestHeader?
For example if we use,
#RequestHeader(value = "abc", required = true)
both parameter and it value has to be there.
Edit:
Suppose i call some rest api has above request header param with "abc" but no value. So in this case i am able to invoke the rest api successfully since i have invoke with "abc" header param even i did not enter a value to it. Due to some governance tool rule, i need to have a specific header param but i dont want force user to enter any value.
Spring 5.2 and lower
The #RequestHeader doesn't provide additional facility to check the value of the parameter to be mandatory i.e. not null.
Given below are the available fields as per Spring Doc
defaultValue: The default value to use as a fallback.
name: The name of the request header to bind to.
required: Whether the header is required; null values are allowed
value: Alias for name()
So what you can do is read the parameter either with the help of #RequestHeader or inject a HttpServletRequest request, read by request.getHeader(...) and check inside the controller method if the value exists and then can call methods to perform the necessary logic.
Although you can make sure that the parameter exists with the help of required attribute for e.g. #RequestHeader(value = "Authorization", required = true) String authorization).
Spring 5.3+
The required field was tightened up, both the property and the value should exist. From the release notes:
The #RequestHeader annotation detect a null conversion result value and treat it as missing. In order to allow an empty value to be injected as a null argument, either set required=false on the argument annotation, e.g. #RequestParam(required=false), or declare the argument as #Nullable.
As #Mudassar said, #RequestHeader doesn't provide additional facility to check the value of the parameter to be mandatory i.e. not null.
It is related to this issue: https://github.com/spring-projects/spring-framework/issues/23939
I developed a workaround for this problem using an annotation #RequestHeaderNonNull that I've created: https://github.com/armandoalmeida/request-header-required-non-null
I hope I've helped you!

Can I get saml-token as string?

I am using spring-security-saml2 1.0.0.RELEASE.
It works well and pretty good for me.
But New requirement is rised. I need saml-token as string.
can I get the saml-token as string. I find saml-token in log.
But how to get the saml-token as string format?
Good question, I've just added a new chapter to the Spring SAML manual which addresses this issue:
Authentication assertion
Assertion used to authenticate user is stored in the
SAMLCredential object under property authenticationAssertion. By
default the original content (DOM) of the assertion is discarded and
system only keeps an unmarshalled version which might slightly differ
from the original, e.g. in white-spaces. In order to instruct Spring
SAML to keep the assertion in the original form (keep its DOM) set
property releaseDOM to false on bean WebSSOProfileConsumerImpl.
Assertion can be serialized to String using the following call:
XMLHelper.nodeToString(SAMLUtil.marshallMessage(credential.getAuthenticationAssertion()))

Response.getMetaData().get("location") in jersey: Why does it return a list?

In my service I am executing the following line:
return Response.created("someuri").build();
Then in my client in order to get the location I have to do
response.getMetaData().get("location").get(0);
This is all good and well, but I am wondering why on earth that is returned as a List instead of just a URI. Can a jersey expert help me out here?
Thanks!
getMetaData() returns a map of Headers in the HTTP response, and while we'd expect only one value per key most of the time, the way the HTTP protocol lists Headers line by line, there is no enforcement that header names have to be unique, so the API reflects that in its MultivaluedMap.
Moreover while we'd expect a unique value for "Location", there are valid use cases for having multiple values for other types of headers such as "Set-Cookie".

need the query string from the request to use with the input attribute of the action mapping

I am using struts 1.3.10. I need the query string from the request to use with the input attribute of the action mapping so that when validation fails, the forward goes to correct page without any null pointer exceptions. how can I do this? I do have the entire forward(myAction.do?foo="bar") as a form property posted throught he jsp. Plesae let me know if I am not clear on the problem definition.
Have you tried request.getQueryString()?
Example:
String queryString = request.getQueryString(); //where "request" is "HttpServletRequest"

Resources