Custom json Converter to fail on unknown properties but the response is not json

79 Views Asked by At

I implemented a custom converter to fail on unknown properties. I have a Front connected to FirstBackend and FirstBackend connected to SecondBackend. i m on a springboot v2.7.14 this is my converter :

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        converters.add(mappingJackson2HttpMessageConverter(objectMapper()));

    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
        MappingJackson2HttpMessageConverter bean = new MappingJackson2HttpMessageConverter();
        bean.setObjectMapper(objectMapper);
        return bean;
    }

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    }

the FirstBackend use this end point:

@Autowired
    private DataWebClient dataWebClient;

@PostMapping(value = "/fullText", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Mono<String>> searchInFullTextMode(@RequestBody FullTextSearchQuery request,
            @RequestParam(value = "searchEngineType") String searchEngineType,
    @RequestParam(defaultValue = "false", required = false) boolean searhcCategory) throws JsonProcessingException {

        if (!StringUtils.hasLength(request.getText()) && searhcCategory) {
            throw new RequiredParameterException("required parameter....");
        }

        Mono<String> result = dataWebClient.searchInFullTextMode(request, searchEngineType);
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result);
    }

the SecondBackend use this to get data :

import org.springframework.web.reactive.function.client.WebClient;
import com.fasterxml.jackson.databind.json.JsonMapper;

private static final JsonMapper MAPPER = JsonMapper.builder().propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE).build();

    @Autowired
    private WebClient webClient;

public Mono<String> searchInFullTextMode(FullTextSearchQuery fullTextSearchQuery, searchEngine)   throws JsonProcessingException {
        return webClient.post().uri(searchEngine.getUrl() + "/fulltext")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(MAPPER.writeValueAsString(fullTextSearchQuery)))
                .retrieve()
                .onStatus(HttpStatus::isError, WebClientHelper::handleDataResponseException)
                .bodyToMono(String.class);
    }

is there a link between reactive manner to get data and the converter ?

Also for the converter i tried to override configureMessageConverters : it fails on unknown properties (so as i want) but the response is not json but string or array of strings and double quote is escaped

"[{\"id\":\"00300111111\",\"type\":{\"id\":\"00f000ABCDRF\",\"businessid\":\"ORG\",\"texts\":[{\"type\":\"LLLLLBA\",\"text\":{\"provenance\":\"DL/DAV\",\"id\":\"0080010bd2oq\",\"type\":\"TEXT\",\"lang\":\"fre\",\"value\":\"Organisation\"}},{\"type\":\"AAO\",\"text\":{\"id\":\"00e000dg3fiq\",\"type\":\"TEXT\",\"lang\":\"fre\",\"value\":\"Toute entité/Agent/Organisation\"}}]},\"activityperiodstartdate\":\"1999-09-07\",......

or

"{\n  \"took\" : 800,\n  \"timed_out\" : false,\n  \"hits\" : {\n    \"total\" : 76594,\n    \"hits\" : [ {\n      \"_id\" : \"00j000f6f6p9\",\n      \"_score\" : 2567.60834,\n      \"_source\" : {\n        \"type_document_par\" : \"Toute entité/Agrégation/Cors/Cors candidat\",\n        \"id\" : \"00j000f6FFFF\",\n        \"text\" : {\n          \"ti\" : {\n            \"provenance\" : [ \"DAV\" ],\n ......  

On the other hand i tried to override : extendMessageConverters instead of configureMessageConverters, in this case the fail on unknown properties (what i want) does not work but the response is nice json :

{
    "took": 447,
    "timed_out": false,
    "hits": {
        "total": 10394,
        "hits": [
            {
                "_id": "00j000f6f6p9",
                "_score": 243.60834,
                "_source": {
                    "type_document_path": "Toute entité/Agrégation/Cors/Cors candid",
                    "id": "00j000FFFFFF",
                    "text": {
                        "ti": {
                            "provenance": [
                                "DAV"
                            ],
                            "id": "00e0FRRRR",
                            "value": "Chir"
                        }
                    },
                    "type_document": "Cors"
                },
                "highlight": {
                    "text.ti.value": [
                        "les <em>Chir</em>"
                    ]
                }
            },....

Appreciate any help !

0

There are 0 best solutions below