cancel
Showing results for 
Search instead for 
Did you mean: 

how to remove key with null value storefront ajax response?

liu_chun_yin
Explorer
0 Kudos

Hi,

I have a question about returning data in storefront extension with Ajax, the below snippet should return a particular product data, but if I did not populate some attributes say ProductReferenceData, it will show in the response with null, my question is can it be removed?

As I can observe, there is no such problem in webservices extesnion (I am using v1), is it some setting of marshaller in Spring web?

@RequestMapping(value = "/ajax", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public ProductData getProductData()
{
..
return productData;
}

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

christoph_probst
Active Participant
0 Kudos

You can try to add the follwing annotation to your data object

 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

or

 @JsonInclude(JsonInclude.Include.NON_NULL)

Depending on what jackson version you are using (1st one for jackson 1 - hybris pre 5.5.1, 2nd one for jackson 2 - hybris 5.5.1)

liu_chun_yin
Explorer
0 Kudos

thanks, can this used in generated object from *-beans.xml ?

christoph_probst
Active Participant
0 Kudos

yes you can use this for generated objects. These objects are generated by using a velocity template and you can define your own templates. See https://wiki.hybris.com/display/release5/Generating+Beans+and+Enums#GeneratingBeansandEnums-Generati... how to do that.

Please up vote answer if it is correct, thank you.

liu_chun_yin
Explorer
0 Kudos

Thank you, this is what I looking for.

Answers (1)

Answers (1)

christoph_probst
Active Participant
0 Kudos

You can register an handler which returns an specific http error if an exception is thrown

     @ResponseStatus(value = HttpStatus.NOT_FOUND)
     @ResponseBody
     @ExceptionHandler({ NotificationNotFoundException.class, CMSItemNotFoundException.class })
     public ErrorListWsDTO handleModelNotFoundException(final Exception ex) {
         final ErrorListWsDTO errorListDto = handleErrorInternal(ex.getClass().getSimpleName(), ex.getMessage());
         return errorListDto;
     }

In this case you can throw an ItemNotFoundException in your method and you will get an 404 error on the client side.

The ErrorListWsDTO is a data object which contains some information about the error. Please be aware that you should not return any information here which can be used for an attack on your website.

If you take a look at the OCC layer of hybris you will find a BaseController which does a similar thing.

liu_chun_yin
Explorer
0 Kudos

oh sorry, I think I did not make it clear. What I want is like if I have two methods using the same Data object but will populate different attributes, so that both methods will return the following data

Method A: "object": { "key1": "value1", "key2": null, "key3": "value3", }

Method B: "object": { "key1": null, "key2": "value2", "key3": "value3", }

My question is can I remove "key2" from MethodA response and "key1" from MethodB ?