I am using MvcUriComponentsBuilder.fromMethodName() to get a List of URLs and return them to the front end. Following is the example output where in i am getting domain in the form of localhost:
[ http://localhost:8081/files/1800_tiger.jpg/slideshow, http://localhost:8081/files/1800_trees.jpg/slideshow ]
Instead of localhost I want MvcUriComponentsBuilder to return me IP address of my system. Following is my code implementation:
@CrossOrigin
@RestController
public class ContentResource {
@RequestMapping("/getAllFiles")
public ResponseEntity<List<String>> getAllFiles(@RequestParam String panelName) {
List<String> fileNamesList = panelFileListMap.get(panelName);
if (fileNamesList != null) {
List<String> allFiles = fileNamesList.stream()
.map(fileName -> MvcUriComponentsBuilder
.fromMethodName(ContentResource.class, "getFile", fileName, panelName).build().toString())
.collect(Collectors.toList());
return ResponseEntity.ok().body(allFiles);
} else {
throw new RuntimeException("No images are uploaded in category = " + panelName);
}
}
@GetMapping("/files/{filename:.+}/{panelName}")
@ResponseBody
public ResponseEntity<Resource> getFile(@PathVariable("filename") String filename,
@PathVariable("panelName") String panelname) {
Resource file = storageService.loadFile(filename, panelname);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
.body(file);
}
}
Found out the solution myself: changed the getAllFiles method like following:
InetAddress ip = null;
@RequestMapping("/getAllFiles")
public ResponseEntity<List<String>> getAllFiles(@RequestParam String panelName) {
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> fileNamesList = panelFileListMap.get(panelName);
if (fileNamesList != null) {
List<String> allFiles = fileNamesList.stream()
.map(fileName -> MvcUriComponentsBuilder
.fromMethodName(ContentResource.class, "getFile", fileName, panelName)
.host(ip.getHostAddress()).build().toString())
.collect(Collectors.toList());
return ResponseEntity.ok().body(allFiles);
} else {
throw new RuntimeException("No images are uploaded in category = " + panelName);
}
}