How to serialize empty string to selfclosing tag - jms-serializer

Using JMS Serializer
I need to get self closing tag setting empty string
<logout/>
I always get
<logout></logout>
I can't use
->setSerializeNull(true)
because the class I'm serializing has many properties that if they are null they can't be serialized
Any idea how to get it done ?

Related

How to get payload values from byte[] message in Spring Integration int-http:outbound-gateway?

I am configuring an outbound-gateway in my context.xml where I want to extract values from a byte[] type payload and use them to construct a URI. I found that SpEL allows you to build it this way:
url-expression="T(org.springframework.web.util.UriComponentsBuilder)
.fromHttpUrl('http://HOST:PORT/PATH')
.queryParams(payload)
.build()
.toUri()"
Source: https://docs.spring.io/spring-integration/reference/html/http.html#mapping-uri-variables
My variation of the solution looks like this:
<int-http:outbound-gateway id="candleRequestGateway"
request-channel="candleRequestChannel"
reply-channel="dataResponseChannel"
http-method="GET"
url-expression="T(org.springframework.web.util.UriComponentsBuilder)
.fromHttpUrl('some/{path}')
.queryParam('myParam', payload.get('myParam'))
.buildAndExpand(payload.get('path'))
.toUri()"/>
However, I am getting the following error which occurs while executing payload.get('myParam') part:
org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#3]; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method get(java.lang.String) cannot be found on type byte[]
I understand and agree with the error. My question: is there a way (a specific SpEL expression(?)) to extract the values from byte[] payload without having to transform it before it reaches the outbound-gateway? Is this a valid solution at all?
Exactly what are you expecting payload.get('myParam') and payload.get('path') to do when the payload is a byte[].
Clearly, a byte[] does not have a get(String) method.
to extract the values from byte[]
Extract how? A byte[] is an unstructured array of bytes; the only thing you can do is something like new String(payload).substring(0, 5) or similar.
If the bytes contain JSON, you could use a #jsonPath SpEL function.

grape-api - Force empty string to set values to null

I am creating an API endpoint which contains a file upload field and a few string fields. My goal is to allow clients to clear values on those string fields, i.e. the DB should persist these values as null.
However, due to the fact that the request may contain files, the client should be setting the Content-type header to multipart/form-data. This implies that client cannot send a representation of "null", but can only send an empty string to indicate the intent of clearing the value for a given string field.
Is there a way for grape-api library to know that when it is receiving a multipart request it should be able to nullify blank string values in the params, or is there a better approach to what I am trying to achieve?
Grape.configure do |config|
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
end
you can override the param builder. extend the default one and override the build_params method or monkey patch it.
params.transform_values {|v| v.eql?('') ? nil : v }

FreeMarker Java 8 Optional value support

Does FreeMarker support Optional value in Java 8?
e.g. I have String id, and its getter method is like:
public Optional<String> getId() {
return Optional.ofNullable(Id);
}
How am I going to reference it in the .ftl file. It seems that {data.id} can not find the correct Optional value. But gives me Optional[1334586]
Well, Freemarker is not supposed to be aware of Optional or it is better to say that its dynamically typed so it works for any object.
Since you calling ${data.id} it's just calls toString on Optional which is totally expected behavior.
If you want to handle null values in your template and for that you want to use Optional, you may choose to set a default value if null, so Optional usage won't be needed:
Synopsis: unsafe_expr!default_expr or unsafe_expr! or (unsafe_expr)!default_expr or (unsafe_expr)!
Example: ${data.id!"No id."}
Or check if it's exists:
<#if data?? && data.id??>
Id found
<#else>
Id not found
</#if>
For more info check out the Freemarker docs. Specifically parts: Handling missing values and Missing value test operator.
If you just want to get the value from Optional in your template:
${data.id.orElse('')}
or
${data.id.get()!''}

<logic:messagesPresent> tag in Struts1 not looping through multiple errors in Action Messages

I have a field in a bean that is failing 2 validations, as such 2 messages are being inserted in ActionMessages with the following command:
validationErrors.add("field1", new ActionMessage("Phone number is greater than 10 digits", false));
validationErrors.add("field1", new ActionMessage("Phone number has invalid characters", false));
Although I see the errors in the ActionMessages object (by setting a breakpoint in the debugger), only the first one gets displayed in my JSP, where I have:
<logic:messagesPresent message="true">
<html:messages id="message" property="field1" message="true">
<logic:present name="message">
<c:out value="${message}"/>
</logic:present>
</html:messages>
</logic:messagesPresent>
Why is only the first message displayed, when <html:messages> should loop through all the messages where the property is "field1"?
I ended up figuring out my issue and it had to do with how I am creating the new ActionMessage.
When you use:
public ActionMessage(<error message>, false)
While it allows you to display a literal value by using the <html:messages> tag in conjunction with either a <bean:write> or <c:out>, it won't iterate over multiple messages for a given property, why I don't know.
I tested and found that if I use a resource bundle and create the ActionMessage with a standard:
public ActionMessage(<key in resource bundle>)
I am able to display multiple messages for a single property.
Unfortunately, because I am using hibernate validator I don't want to use a resource bundle and struts to replace the values (would rather have the hibernate validator annotation replace values) and will likely just display a single message at a time for now.
Struts <logic:messagesPresent> tag checks that the messages exist on the current request.
Messages are found in the request under the key Globals.MESSAGE_KEY. If you use attribute message only messages are checked.
By default the tag will retrieve the request scope bean it will
iterate over from the Globals.ERROR_KEY constant string, but if this
attribute is set to true the request scope bean will be retrieved
from the Globals.MESSAGE_KEY constant string. Also if this is set to
true, any value assigned to the name attribute will be ignored.
The <html:messages> tag is used to display the messages if specified attribute message is true.
Now you have used a property attribute that filters messages for the given property.
Name of the property for which messages should be displayed. If not
specified, all messages (regardless of property) are displayed.
If you have only one message with the field1 property, then only one message will be displayed.
Look here how can you use action messages object
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.common.field1.required");
saveMessages(request, messages); // storing messages as request attributes
Properties file:
error.common.field1.required = Field1 is required.
And display messages
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>
It will loop all massages under the global message key. If you want to use custom key you can use it with the parameter of ActionMessage
messages.add("field1", new ActionMessage("error.common.field1.required");
to retrieve the message
<logic:messagesPresent message="true">
<html:messages id="message" property="field1" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>

parse out JSON.parse(cookies[:cookie_name]) item into ruby variable

I need to access a specific string inside the cookie.
I am using:
#id = JSON.parse(cookies[:cookie_name])
and it returns a JSON object. What is the best way to access a specific item in the JSON object?
Here is example of the JSON object:
{"distinct_id"=>"12345", "$initial_referrer"=>"$direct", "$initial_referring_domain"=>"$direct", "__mps"=>{}, "__mpso"=>{}, "__mpa"=>{}, "__mpap"=>[], "mp_name_tag"=>"12345", "id"=>"12345"}
What I want from that object is the distinct_id.
Any direction would be great.
That looks more like a Ruby hash than a JSON object (JSON.parse returns a hash as well). If that's the case, wouldn't #id['distinct_id'] work?

Resources