I am using return ResponseEntity of Spring to return HTTP response.
While passing POJO or MAP in entity, it converts that to JSON Object. Like
return new ResponseEntity<Object>(result, HttpStatus.OK);
result may be POJO class. (getter and setters)
I am using
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.GsonHttpMessageConverter ">
</bean>
Now what i want is, i will get a list of response fields in request. I just want to response the request with those fields only.
For Ex, if i receive the response fields as name,id. Then the object must contain those two only.
{
"name":"test",
"id":15
}
i can't able to use #jsonignore or #explore annotations. B'Coz i want the JSON to be done dynamically using coding.
May be by overriding the GSON convertor methods or using AOP.
Is there any way to configure the adapter of jackson to create the object based on the fields.
If you can switch to jackson probably you can use this little extension i wrote for exactly this purpose:
https://github.com/Antibrumm/jackson-antpathfilter
Related
I`m using Play Framework 1.5 and I'm new in this framework. How can I retrieve the body as JSON when i sent a request body with JSON:
{
"inputNo": 111111,
"name": "検証 太郎"
}
AFAIK there is no built in JSON binding support in Play1x. You can implement binding process manually by using #Before annotation in controller class. In the #Before annotated method, get the json from request body (params.get("body")), parse it using GSON or any other JSON library, and store the pojo in the request object (request.args.put(name, pojo)). Later in the controller method get the pojo from request args (request.args.get(name)). You can define custom annotations to limit this behaviour.
A good implementation would be creating a base controller class with #Before annotation and making this process generic using types.
RPC in Internet transport layer, use dto is reasonable. Http controller? If all controller are used by front end, parameter defined as VO?
I guess you are asking whether the argument of the rest controller method can be a DTO.
Well it will depend on the framework you use. The http parameters are strings.
If the framework has an utility mechanism (probably an annotation) that lets you map the http params you receive into a DTO you supply as the rest controller method arg, there's no problem in the arg being a DTO.
If the framework doesn't have such utility (it just maps each http param into an string arg of the rest controller method), then you have to build manually the DTO in the rest controller method.
I don't know if Spring has such an utility annotation similar to #PathVariable but for gathering multiple request params into a DTO object.
UPDATE:
Spring #RequestBody annotation deserializes the JSON into the java object argument of the rest controller method. So, the arg annotated with #RequestBody is a DTO.
DDD says nothing about which type must be the params of a rest api. They can be either a DTO or Strings, it doesn't matter. If they where strings, you would have to construct the DTO by yourself. Using #RequestBody, Spring framework does it for you.
In java, an object that is carries between process is named following the camel case notation and having the DTO suffix.
e.g. ServiceMessageDTO
DTO stands for data transfer object.
This applies also to the request body parameters from the rest webmethods.
I have seen similar answer here:
Spring MVC: Complex object as GET #RequestParam
Spring map GET request parameters to POJO automatically
I really can't find document of this because this auto mapping is not even done by any annotation. (it doesn't even need #RequestParam in fact)
1) so far I only see simple mapping, the object contain all primitive data, how about if my request is a complicated JSON object which contain several level of attributes (a object contain other objects)? Will the auto-mapping still work?
2) so far I only see Spring controller can take in one auto-map object, can I let it auto-map more than one object? For example:
public #ResponseBody List<MyObject> myAction(MyObject myObject,
MyObject2 myObject2) { ... }
Anyone know where is the document to describe how the mapping work behind the scene? Based on my second question, if Spring allow to do such thing, what if I have same attributes name in MyObject and MyObject2, how the mapping will do?
If you do things like this:
public #ResponseBody List<MyObject> myAction(#RequestBody MyObject myObject) { ... }
Of course you can only have one body in your http request.
As long as you have Jackson in your classpath (spring boot will add this automatically) your objects will be marshalled correctly.
If the JSON in your body is incorrect you will get a 400 (Invalid Request) returned.
I'm in a current situations in which I have a REST endpoint that accepts POST of incoming JSON messages.
The thing is that I don't think I can specify the POJO object so Jackson can marshall the JSON into the POJO object. Reason for this is that I don't have control of what comes to that endpoint, and number of fields and type can change over time, thus, defining a POJO before hand seems not an option.
So I guess the question is....can I simply tell Jackson to don't do any marshalling and give the String of the response? I can deal with that later with JSONObject-JSONArray or Gson maybe. Say I'd have a method like this:
#POST
#Path("/callback")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response facebookUpdate(String json) {
//Do something with the json response...
}
If this is not feasible with Jersey-JAX...any other alternatives?
Thanks!
Alejandro
The easiest is to simply not inject the json into the method and use the request object instead:
public Response facebookUpdate(#Context request) {
try(InputStream is=request.getEntityInputStream()) {
...
}
}
From the request you can then get an inputstream for the request and parse it whichever way you like.
For parsing I can recommend my own jsonj library, which was written specifically to support open ended scenarios like you describe and uses jackson to deserialize into heavily customised implementations of java.util.Map and java.util.List. Gson is also a very solid choice.
If you want to do this application wide, you can instead write your own #Provider and do the same there. This is how I use my library currently actually.
I have a request(POST or GET), having one variable "data" (coming from Xcode)
now data have assigned JSON object
data={"method":"Auth","action":"login"}
now I have a bean having exactly these two fields(i.e. method,action)
now in Spring controller I have method "login".
I want this method to be invoked based on value in "action" of the request JSON object.
Now, I'm not getting what exactly types of annotation i should use.
Please help..
You need to add Jackson to the classpath, and add <mvc:annotation-driven> to your Spring context, then Spring will automatically register a MappingJacksonHttpMessageConverter for JSON Objects.
Now Annotate your method like this:
#ResponseBody
#RequestMapping("/some/path")
public YourResponseObject someName(#RequestBody YourRequestObject data){
// do something here
}
(Both YourRequestObject and YourResponseObject will be automatically converted to and from JSON, but this works only for POST requests AFAIK)