我一直在学习春天,为了使事情变得更紧密,我正在制作一个电子商务应用程序。我已使用rest api连接客户端和服务器。现在,我需要将图像发送到客户端。我的图像已经存储在src / resources文件夹中。我需要知道的是如何通过Rest API发送这些图像。这样我就可以在客户中使用它
我对此很不高兴。我尝试了谷歌,我所能找到的就是将图像文件上传到服务器的示例。我找不到通过rest api从服务器向客户端发送文件的示例。我已经被困在这三天了
这是我的休息控制器:现在我需要知道下一步该怎么做才能发送图像
@RestController
@RequestMapping("/api")
public class CategoriesRestController {
// autowire customer service
@Autowired
private CategoriesService service;
//add mapping for GET all customer
@GetMapping("/categories")
public List<Categories> getCategories() {
return service.getCategories();
}
// adding mapping for GET only one customer
@GetMapping("/categories/{categoryId}")
public Categories getCategory(@PathVariable int categoryId) {
Categories categories = service.getCategory(categoryId);
if(categories == null) {
throw new CustomerNotFoundException("Customer id not found- "+ categoryId);
}else {
return categories;
}
}
// adding mapping for POST/customer i.e. insert a customer
@PostMapping("/categories")
public Categories addCategories(@RequestBody Categories theCategories) { //@RequestBody will convert JSON to JAVA object
// just to make things clear... always set id to 0 when inserting new object
// so that it will be created instead of update
theCategories.setId(0);
service.saveCategories(theCategories);
return theCategories;
}
您可能会以错误的方式思考问题。HTML不需要通过其余的API发送图像本身,而是仅需要图像的路径。您将图像存储在目录中,并且可以将图像的路径传递到HTML。您可以将变量“ imagePath”添加到“类别”,HTML可以在标记中引用它
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句