Monday, 25 July 2022

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format

  let map = new Map<string, string>()  

   map.set(this.getFilename("filePath"), JSON.stringify(fileData));

   map.set("second.xml", JSON.stringify(fileData));  

  

    let jsonObject = {};  

    map.forEach((value, key) => {  

        jsonObject[key] = value  

    });  

    console.log(JSON.stringify(jsonObject)) 

we can pass this JSON.stringify(jsonObject) to the API which accepts hashmap in its request body 

 

How to pass a simple HashMap to the backEnd API

JsonMap will be a simple map which contains some key and a value like

"testData" : "30"

"sampleData" : "40"

let jsonObject = {};
    jsonMap.forEach((value, key) => {
      jsonObject[key] = value
    });

 And finally while posting in the request body we stringify and then send the map

JSON.stringify(jsonObject)

Tuesday, 5 July 2022

File upload test

 Path to test resources

java.nio.file.Path;=

Path resourceDirectory = Paths.get("src", "test", "resources");

String filePath = resourceDirectory.toFile().getAbsolutePath();

File f = new File(absolutePath);

File[] list = f.listFiles();


InputStream targetStream = new FileInputStream(list[0]);


MockMultipartFile file = new MockMultipartFile("file", "sampletest.xml", MediaType.TEXT_PLAIN_VALUE,

targetStream);


MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

mockMvc.perform(multipart("/uploadFile").file(file).param("filePath", absolutePath)).andExpect(status().isOk());

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...