4

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.

6
  • You mean you want to sent response that is a object containing what values you have received in request correct ? Is number of request values is fixed or dynamic ? Commented Sep 10, 2015 at 12:15
  • s exactly. what are all the fields in the request need to be sent as object (POJO already have those fields. But lot other than that. we need to exclude). It is dynamic. We don't know what will come. Commented Sep 10, 2015 at 12:22
  • Do you need to use Gson or can you switch to Jackson? Commented Sep 10, 2015 at 12:32
  • Ya no problem. may use jackson as well. Is there any way to do in jackson. Actually, first i used jackson only. For this only i shifted to GSON. Commented Sep 10, 2015 at 12:37
  • Is POJO class created dynamically? Or you just want to ignore null values? Commented Sep 10, 2015 at 13:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks martin. As per your examples in github, i have to add objectmapper in every method, which works after conversion . I there anyway to tell jackson before converting.
I did create a spring bean encapsulating the neccessary code with a helper method. bean.write(obj, "filters",...). I did not yet try to incorporate it with responsebody. My controllers return a string directly.
The mixin configuration can be done only once as it works on Object. In the @PostConstruct of the spring bean for example.
Ok. What about objectmapper. Can we configure that to achieve this.
I think i will add an example on the github readme. Give me some time ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.