Search code examples
struts2camelcasingstruts2-json-plugin

Can struts2-json-plugin accept upper camel case parameters?


I'm using struts2(struts2-json-plugin) to handle Content-Type=application/json request,

everything is fine where params style is lower camel case,like {"region":"China","version":"v4"}.

But it can't work when params is upper camel case,like {"Region":"China","Version":"v4"}.

I've already read struts docs(https://struts.apache.org/plugins/json/),and still not find the way to solve.

this is part of my struts.xml:

this


Solution

  • It can't work with case insensitive json objects because names are translated into Java class variables of the action class that are case sensitive.

    The json plugin contains the interceptor named json which you should configure to the action to deserialize json content from the request. This answer points to some classes responsible for serialization/deserialization and populating Java objects.

    If you don't want to populate the action then you should not use this interceptor. Instead manually parse the request with this or any other third party library to get the JSONObject. Or you could rewrite the interceptor and comment that code that is using JSONPopulator but deserialize the object with JSONUtil.

    If you want to transform json objects with lower case names to camel case then you should override one of these classes before the population process to correspond to Java class variables names.