D
D
DeNissss44442021-08-03 09:22:35
Java
DeNissss4444, 2021-08-03 09:22:35

How to make a web service that accepts an abstract JSON object in which there will be a regular list?

I have a web service in Spring, it must accept a JSON object in which there will be a regular list with which I need to work further. The difficulty arose with the fact that I don’t understand how I can get some kind of abstract JSON object and not from a specific site ... I used to write a similar web service that also received a JSON sheet object, but from a specific site! And I don’t understand how to make a web service that receives an abstract JSON. Tell me please.

An example of how I implemented a service to get a JSON object from a specific site.

HttpClient httpClient = HttpClientBuilder.create().build();
    ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    private RestTemplate restTemplate = new RestTemplate(requestFactory);

    public List<SiteObj> getSites() {

        String webSiteUrl = "https://api.stackexchange.com/2.2/sites?page=1&pagesize=999&filter=!FmdZfVGXZN9h5PtNfuvm(ASXcf";

        SitesDTO response = null;
        try {
            response = restTemplate.getForObject(new URI(webSiteUrl), SitesDTO.class);

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return response.getItems();
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-08-03
@DeNissss4444

Your code itself reads data from another site. And you need a service that will receive data from outside.
Something like

@RestController
@RequestMapping(produces = APPLICATION_JSON_UTF8_VALUE)
public class MyController {

    @PostMapping("process-json")
    @ResponseBody
    public ResponseEntity<String> processJson(@RequestParam(value = "stringArray")List<String> stringArray) {
        return process(stringArray);
    }
}

C
calculator212, 2021-08-03
@calculator212

"Create a web service that
accepts a json object in the form of an array of strings. The array check is initialized by a REST API call, in the input parameters of which an array of strings is passed (the data transfer format is
JSON). "

Here it is necessary to clarify the task and not to think it out.
What's the difference from how many sites to accept, if you have this JSON will have one structure. JSON is sent to you and you process it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question