{"id":596,"date":"2023-03-23T21:28:47","date_gmt":"2023-03-23T13:28:47","guid":{"rendered":""},"modified":"2023-03-23T21:28:47","modified_gmt":"2023-03-23T13:28:47","slug":"SpringBoot REST\u793a\u4f8b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/596.html","title":{"rendered":"SpringBoot REST\u793a\u4f8b"},"content":{"rendered":"
\n
\u521b\u5efa\u4e94\u4e2a\u53d8\u91cfid\uff0cpname\uff0cbatchno\uff0cprice\u548cnoofproduct\u3002<\/span> <\/code>\n <\/div>\n \u4f7f\u7528\u6ce8\u89e3 @RestController <\/strong>\u6ce8\u89e3\u7c7b\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n \u4f7f\u7528\u6ce8\u89e3 @Service <\/strong>\u6ce8\u89e3\u7c7b\uff0c\u5e76\u5b9e\u73b0 IProductService <\/strong>\u63a5\u53e3\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/body>
\n \u521b\u5efa\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\u3002<\/span>
\n \u4f7f\u7528\u5b57\u6bb5\u751f\u6210\u6784\u9020\u51fd\u6570<\/strong>\u3002
\u53f3\u952e\u5355\u51fb\u6587\u4ef6->\u6e90->\u4f7f\u7528\u5b57\u6bb5\u751f\u6210\u6784\u9020\u51fd\u6570->\u5168\u9009->\u751f\u6210<\/span>
\n \u751f\u6210\u5b57\u6bcd<\/strong>\u548csetter\u3002<\/span> <\/p>\n\npackage <\/span>com.lidihuo;\npublic <\/span>class <\/span>Product \n{\nprivate <\/span>int <\/span>id;\nprivate <\/span>String pname;\nprivate <\/span>String batchno;\nprivate double <\/span>price;\nprivate <\/span>int <\/span>noofproduct;\n\/\/default <\/span>constructor\npublic <\/span>Product()\n{\n \n}\n\/\/constructor using fields\npublic <\/span>Product(int <\/span>id, String pname, String batchno, double <\/span>price, int <\/span>noofproduct) \n{\nsuper();\nthis<\/span>.id = id;\nthis<\/span>.pname = pname;\nthis<\/span>.batchno = batchno;\nthis<\/span>.price = price;\nthis<\/span>.noofproduct = noofproduct;\n}\n\/\/getters and setters\npublic <\/span>int <\/span>getId() \n{\nreturn <\/span>id;\n}\npublic void <\/span>setId(int <\/span>id) \n{\nthis<\/span>.id = id;\n}\npublic <\/span>String getPname() \n{\nreturn <\/span>pname;\n}\npublic void <\/span>setPname(String pname) \n{\nthis<\/span>.pname = pname;\n}\npublic <\/span>String getBatchno() \n{\nreturn <\/span>batchno;\n}\npublic void <\/span>setBatchno(String batchno) \n{\nthis<\/span>.batchno = batchno;\n}\npublic double <\/span>getPrice() \n{\nreturn <\/span>price;\n}\npublic void <\/span>setPrice(double price) \n{\nthis<\/span>.price = price;\n}\npublic <\/span>int <\/span>getNoofproduct() \n{\nreturn <\/span>noofproduct;\n}\npublic void <\/span>setNoofproduct(int <\/span>noofproduct) \n{\nthis<\/span>.noofproduct = noofproduct;\n}\n}\n<\/pre>\n
\n com.lidihuo <\/strong>\u5305\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aController\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u79f0\u4e3a
\n ProductController <\/strong>\u7684\u63a7\u5236\u5668\u3002\n <\/div>\n
\n \u6211\u4eec\u5df2\u81ea\u52a8\u8fde\u63a5 IProductService <\/strong>\u63a5\u53e3\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u6b65\u4e2d\u521b\u5efa\u5b83\u3002<\/span>
\n \u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u6ce8\u89e3 @GetMapping <\/strong>\u521b\u5efa\u4e86\u4e00\u4e2a\u6620\u5c04\/\u4ea7\u54c1<\/strong>\u3002<\/span>
\n \u6211\u4eec\u5df2\u5c06\u65b9\u6cd5 getProduct()<\/strong>\u6620\u5c04\u5230\/product <\/strong>\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4ea7\u54c1\u5217\u8868\u3002<\/span> <\/p>\n\npackage <\/span>com.lidihuo;\nimport java.util.List;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n@RestController\npublic <\/span>class <\/span>ProductController \n{\n@Autowired\nprivate <\/span>IProductService productService;\n\/\/mapping the getProduct() method to \/product\n@GetMapping(value = \"\/product\"<\/span>)\npublic <\/span>List<Product> getProduct() \n{\n\/\/finds all the products\nList<Product> products = productService.findAll<\/span>();\n\/\/returns the product list\nreturn <\/span>products;\n}\n}\n<\/pre>\n
\n com.lidihuo <\/strong>\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n IProductService <\/strong>\u7684\u63a5\u53e3\uff0c\u5e76\u5b9a\u4e49
\n findAll() <\/strong>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4ea7\u54c1\u5217\u8868\u3002\n <\/div>\n\npackage <\/span>com.lidihuo;\nimport java.util.List;\npublic <\/span>interface IProductService \n{\nList<Product> findAll();\n}\n<\/pre>\n
\n Service <\/strong>\u7c7b\u3002\u6211\u4eec\u5728\u5305
\n com.lidihuo <\/strong>\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a
\n ProductService <\/strong>\u7684\u670d\u52a1\u7c7b\u3002\n <\/div>\n
\n \u5728\u6b64\u7c7b\u4e2d\uff0c\u4f7f\u7528\u6ce8\u89e3 @Override <\/strong>\u8986\u76d6 findAll()<\/strong>\u65b9\u6cd5\u3002 ProductService\u7c7b\u7684findAll()\u65b9\u6cd5\u5c06\u8986\u76d6 IProductService <\/strong>\u63a5\u53e3\u7684findAll()\u65b9\u6cd5\u3002<\/span>
\n \u521b\u5efa ArrayList <\/strong>\u7684\u5bf9\u8c61\u3002<\/span>
\n \u6dfb\u52a0<\/strong>\u9635\u5217\u5217\u8868\u4e2d\u7684\u4ea7\u54c1\u3002<\/span>
\n \u8fd4\u56de\u4ea7\u54c1\u7684\u5217\u8868<\/strong>\u3002<\/span> <\/p>\n\npackage <\/span>com.lidihuo;\nimport java.util.ArrayList;\nimport java.util.List;\nimport org.springframework.stereotype.Service;\n@Service\npublic <\/span>class <\/span>ProductService implements <\/span>IProductService \n{\n@Override\npublic <\/span>List<Product> findAll()\n{\n\/\/creating an object of ArrayList\nArrayList<Product> products = new <\/span>ArrayList<Product>();\n\/\/adding products to the List\nproducts.add<\/span>(new Product(100, \"Mobile\"<\/span>, \"CLK98123\"<\/span>, 9000.00, 6));\nproducts.add<\/span>(new Product(101, \"Smart TV\"<\/span>, \"LGST09167\"<\/span>, 60000.00, 3));\nproducts.add<\/span>(new Product(102, \"Washing Machine\"<\/span>, \"38753BK9\"<\/span>, 9000.00, 7));\nproducts.add<\/span>(new Product(103, \"Laptop\"<\/span>, \"LHP29OCP\"<\/span>, 24000.00, 1));\nproducts.add<\/span>(new Product(104, \"Air Conditioner\"<\/span>, \"ACLG66721\"<\/span>, 30000.00, 5));\nproducts.add<\/span>(new Product(105, \"Refrigerator \"<\/span>, \"12WP9087\"<\/span>, 10000.00, 4));\n\/\/returns a list of product\nreturn <\/span>products;\n}\n}\n<\/pre>\n
\n static <\/strong>\u6587\u4ef6\u5939(src\/main\/resources\/static)\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aHTML\u6587\u4ef6\u3002\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a
\n index <\/strong>\u7684 HTML \u6587\u4ef6\u3002\u5728\u6b64\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86
\n \u83b7\u53d6\u6240\u6709\u4ea7\u54c1<\/strong>\u7684\u94fe\u63a5\u3002\n <\/div>\n\n<!DOCTYPE html>\n<html>\n<head>\n<title>Home page<\/title>\n<meta charset=\"UTF-8\"<\/span>>\n<meta name=\"viewport\"<\/span> content=\"width=device-width, initial-scale=1.0\"<\/span>>\n<\/head>\n<body>\n<p>\n<a href=\"product\"<\/span>>Get all Products<\/a>\n<\/p>\n<\/body>\n<\/html>\n<\/pre>\n
<\/p>\n
\n SpringBootRestExampleApplication.java <\/strong>\u6587\u4ef6\u5e76\u4ee5Java \u5e94\u7528\u7a0b\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u8fd0\u884c\u5728\u7aef\u53e3
\n 8080 <\/strong>\u4e0a\u3002\n <\/div>\n\npackage <\/span>com.lidihuo;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n@SpringBootApplication\npublic <\/span>class <\/span>SpringBootRestExampleApplication \n{\npublic <\/span>static void <\/span>main(String[] args) \n{\nSpringApplication.run<\/span>(SpringBootRestExampleApplication.class, args);\n}\n}\n<\/pre>\n
<\/p>\n
\n \u83b7\u53d6\u6240\u6709\u4ea7\u54c1<\/strong>\u7684\u94fe\u63a5\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002\n <\/div>\n <\/p>\n
\n \u83b7\u53d6\u6240\u6709\u4ea7\u54c1<\/strong>\u3002\u5b83\u4ee5
\n JSON <\/strong>\u683c\u5f0f\u8fd4\u56de\u4ea7\u54c1\u5217\u8868\uff0c\u5e76\u4e14URL\u66f4\u6539\u4e3ahttp:\/\/localhost:8080\/product\u3002\n <\/div>\n <\/p>\n
\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringBoot REST\u793a\u4f8bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-bc_springboot_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/596"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}