Ehcache in Spring MVC with parameters - spring-mvc

Dear all,
I am using EhCache with Spring MVC 3. My server takes json from other server, I have added cache on it. It works fine when there is no parameters. Can I cache the json based on the parameters also.
Right now I am using method name as key for caching, can I include params also ??
Here is code
#Cacheable(value="products", key="#root.method.name")
Thanks,
Dev

By default, as described in Spring's Cache abstraction, the key will be computed from the parameters.
So try first with dropping the key attribute in your declaration and see what that does for you.
If you need to tweak the key computation, have a look at the documentation.

Related

Axon stores events as XML instead of JSON

I'm new on Axon framework and i want to configure Axon in order to store events as JSON.
I'm using Spring-Boot and, as per documentation, i set some key/value in a .properties file
I expected to see events stored as JSON but i keep seeing XML in the serializedPayload of the MongoDB event store.
I think I have configured the serilializers correctly but obviously I'm doing something wrong
Please note that debugging the application it seems that JacksonSerializer class is never invocated and instead XStreamSerializer class is called even my key/value configuration.
So i need to understand why my jackson configuraton is not calling the right serializer
You have to configure the MongoEventStorageEngine to use the Serializer you want.
Looking at it's Builder (specially the javadoc), you can see it offers a snapshotSerializer and a eventSerializer methods while the javadoc states that both of them defaults to XStreamSerializer.
If you need help configuring it, I can point you to the mongo-axon-example where you can see a project configured and running.
The example is not using Jackson but it should be an easy addition to configure it here.
Even if the documentation doesn't seem to mention it, it is not enough to add the key/value in the .properties file. You need also to add the JacksonSerializer to the MongoEventStorageEngine as following:

Rest API - partial update

I'm designing a REST API (implemented with ASP.NET Web API, not ASP.NET core, because we can't upgrade it right now).
I'm looking for the best solution to partially update an entity.
For example, the "Person" entity has 100 properties, and a third party system only has to update the "PersonValidityDate".
What is the best and simplest solution to do it ?
I read some article about the PATCH verb, with JSON patch or JSON merge patch, but it seems a bit complicated and not supported by everyone... Is it ?
I'm looking for a very simple solution to do it, that any integrator can understand and use...
What do you suggest ? Is PATCH often used?
If I use POST/PUT and only give the properties to update and ignore the other properties, when the body is deserialized, nullable properties will be null. How can I distinguish the properties set to NULL explicitely and not provided properties ?
According to RFC 5789
Several applications extending the Hypertext
Transfer Protocol (HTTP) require a feature to do partial resource
modification. The existing HTTP PUT method only allows a complete
replacement of a document. This proposal adds a new HTTP method,
PATCH, to modify an existing HTTP resource.
So you should use a PATCH request for partial update of the object and PUT for complete object replacement.
And you should not use the complete model in patch request with empty/null fields instead use the new model for PATCH request.
You can check more example here

Is there a better way in Spring MVC to pass a cookie in to an endpoint where the cookie name is defined at runtime?

I'm using Spring MVC to produce a REST service. Most of my #RequestMappings require a cookie which contains a session ID. The name of the cookie depends on the environment in which the service is deployed. So for example I use 'dev_session' in dev environments and 'prod_session' in production.
As far as I can tell, this rules out using #CookieValue since the name is a constant. Other options I've come up with are:
Passing in HttpServletRequest and calling getCookies(). This is
pretty ugly, especially as I would need to do it for about 100
request mappings
Use AOP #Around advice to get the HttpServletRequest, find the cookie and then pass it in to target method. I'm not sure if this would work and whether i'd have to have both the cookie value and the HttpServletRequest as a parameter to the target method so that i'd have HttpServletRequest available. Either way, it doesn't seem like a good approach.
WebArgumentResolver. This is the best option i've come up with and I have it working. I annotate the parameter with a custom annotation #SessionCookie then in WebArgumentResolver.resolveArgument() if the argument has that annotation I return the value of the cookie. Unfortunately it breaks the auto-generated documentation I use as it doesn't recognise the annotation (Swagger-springmvc). Also it still seems rather hard work for something so simple.
So my question is, is there a better way to get hold of a cookie with a name only known at runtime?

What happens if web services changes the parameters?

Say i'm having a web service that accepts two arguments and that is being called/consume in my application. Now after some time, the web service changes and accepts three arguments, hence in my application, would that be throwing an error, or i need to just update the web reference, or i need to recreate a web serivce or would that be working fine?
Let me know if any doubts
Thanks!
You could add optional parameters where if a parameter value isn't given to the method a default value is used.
From a "pure" architectural aspect, you should never change the signature of a method of a service once it's in use. You should version by creating a new method with a different namespace. But staying pure is sometimes difficult to do.
In your case you need to update the Web reference in the client application and then modify the code to pass in the appropriate parameter to the method in the service proxy.

IIS Integrated Request Processing Pipeline -- Modify Request

I want to implement an ISAPI filter like feature using HttpModule in IIS7 running under IIS Integrated Request Processing Pipeline mode.
The goal is to look at the incoming request at the Web Server level, and inject some custom HttpHeaders into the request. (for ex: HTTP\_EAUTH\_ID)
And later in the page lifecycle of an ASPX page, i should be able to use that variable as
string eauthId = Request.ServerVariables["HTTP\_EAUTH\_ID"].ToString();
So implementing this module at the Web Server level, is it possible to alter the ServerVariables collection ??
HttpRequest.ServerVariables Property is a read-only collection. So, you cannot directly modify that. I would suggest storing your custom data in httpcontext (or global application object or your database) from your httpmodule and then reading that shared value in the aspx page.
If you still want to modify server variables, there is a hack technique mentioned in this thread using Reflection.
I believe the server variables list only contains the headers sent from the browser to the server.
You won't be able to modify either the HttpRequest.Headers or the HttpRequest.ServerVariables collection. You will however be able to tack on your information to any of:
HttpContext.Current.Items
HttpContext.Current.Response.Headers
Unfortunately, Request.Params, Request.QueryString, Request.Cookies, Request.Form (and almost any other place you'd think of stuffing it is read only.
I'd strongly advise against using reflection if this is a HttpModule you're planning on installing into IIS 7. Given that this code will be called for (potentially) every request that goes through the web server it'll need to be really fast and reflection just isn't going to cut it (unless you have very few users).
Good luck!

Resources