{"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 <\/head>
\n <\/p>\n

\n

SpringBoot REST\u793a\u4f8b<\/h1>\n<\/p><\/div>\n
\n REST\u5e94\u7528\u7a0b\u5e8f\u9075\u5faaREST\u4f53\u7cfb\u7ed3\u6784\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528REST\u5e94\u7528\u7a0b\u5e8f\u6765\u5f00\u53d1\u548c\u8bbe\u8ba1\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u751f\u6210\u5bf9\u6570\u636e\u6267\u884cCRUD\u64cd\u4f5c\u7684HTTP\u8bf7\u6c42\u3002\u901a\u5e38\uff0c\u5b83\u4ee5JSON\u6216 XML \u683c\u5f0f\u8fd4\u56de\u6570\u636e\u3002\n <\/div>\n

Spring Boot REST API\u793a\u4f8b<\/h2>\n
\n \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2aREST\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u6b64\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4ea7\u54c1\u5217\u8868\u5e76\u8fd4\u56de\u76f8\u540c\u7684\u5217\u8868\u3002\u5b83\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\u6570\u636e\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u5728RSET\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u5b83\u5e76\u4e86\u89e3REST\u65b9\u6cd5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\u3002\n <\/div>\n
\n \u6b65\u9aa41: <\/strong>\u6253\u5f00 Spring Initializr https:\/\/start.spring.io\/\u3002\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u9009\u62e9 Spring Boot\u7248\u672c
\n 2.3.0.M2 <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa43: <\/strong>\u63d0\u4f9b
\n \u7ec4<\/strong>\u540d\u79f0\u3002\u6211\u4eec\u63d0\u4f9b\u4e86\u7ec4\u540d
\n com.lidihuo <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u63d0\u4f9b
\n \u5de5\u4ef6<\/strong>\u3002\u6211\u4eec\u63d0\u4f9b\u4e86
\n Artifact spring-boot-rest-example <\/strong>\u793a\u4f8b\u3002\n <\/div>\n
\n \u6b65\u9aa45: <\/strong>\u6dfb\u52a0
\n Spring Web <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n \u6b65\u9aa46: <\/strong>\u5355\u51fb\"\u751f\u6210\"\u6309\u94ae\u3002\u5f53\u6211\u4eec\u5355\u51fb
\n Generate <\/strong>(\u751f\u6210)\u6309\u94ae\u65f6\uff0c\u5b83\u5c06\u4e0e\u5e94\u7528\u7a0b\u5e8f\u76f8\u5173\u7684\u6240\u6709\u89c4\u8303\u5305\u88c5\u5230
\n Jar <\/strong>\u6587\u4ef6\u4e2d\uff0c\u5e76\u5c06\u5176\u4e0b\u8f7d\u5230\u672c\u5730\u7cfb\u7edf\u3002\n <\/div>\n
\n \u6b65\u9aa47: <\/strong>
\n \u63d0\u53d6<\/strong> jar\u6587\u4ef6\u3002\n <\/div>\n
\n \u6b65\u9aa48: <\/strong>\u590d\u5236\u6587\u4ef6\u5939\u5e76\u5c06\u5176\u7c98\u8d34\u5230STS\u5de5\u4f5c\u533a\u4e2d\u3002\n <\/div>\n
\n \u6b65\u9aa49: <\/strong>
\n \u5bfc\u5165<\/strong>\u9879\u76ee\u3002\n <\/div>\n
\n \u6587\u4ef6->\u5bfc\u5165->\u73b0\u6709Maven\u9879\u76ee->\u4e0b\u4e00\u6b65->\u6d4f\u89c8- >\u9009\u62e9\u6587\u4ef6\u5939spring- spring-boot-rest-example->\u9009\u62e9\u6587\u4ef6\u5939->\u5b8c\u6210\n <\/div>\n
\n \u5bfc\u5165\u9879\u76ee\u9700\u8981\u65f6\u95f4\u3002\u6210\u529f\u5bfc\u5165\u9879\u76ee\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728IDE\u7684
\n Package Explorer <\/strong>\u90e8\u5206\u4e2d\u770b\u5230\u5b83\u3002\n <\/div>\n
\n \u6b65\u9aa410: <\/strong>\u521b\u5efa\u4e00\u4e2a
\n \u6a21\u578b<\/strong>\u5305
\n com.lidihuo <\/strong>\u4e2d\u7684\u7c7b\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a
\n \u4ea7\u54c1<\/strong>\u7684\u7c7b\u3002\u5728\u6b64\u7c7b\u4e2d\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c:\n <\/div>\n

\u521b\u5efa\u4e94\u4e2a\u53d8\u91cfid\uff0cpname\uff0cbatchno\uff0cprice\u548cnoofproduct\u3002<\/span>
\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

\n \u53f3\u952e\u5355\u51fb\u6587\u4ef6->\u6e90->\u751f\u6210Getter\u548cSetters->\u5168\u9009->\u751f\u6210\n <\/div>\n
\n \u5b8c\u6210\u6240\u6709\u6b65\u9aa4\u540e\uff0c\u7c7b\u5982\u4e0b\u6240\u793a\u3002\n <\/div>\n
\n Product.java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u3002\n <\/div>\n
\n \u6b65\u9aa411: <\/strong>\u5728
\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

\u4f7f\u7528\u6ce8\u89e3 @RestController <\/strong>\u6ce8\u89e3\u7c7b\u3002<\/span>
\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

\n ProductController.java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa412: <\/strong>\u5728\u5305
\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
\n IProductService.java <\/strong>\n <\/div>\n
\n
\npackage <\/span>com.lidihuo;\nimport java.util.List;\npublic <\/span>interface IProductService \n{\nList<Product> findAll();\n}\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa413: <\/strong>\u521b\u5efa\u4e00\u4e2a
\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

\u4f7f\u7528\u6ce8\u89e3 @Service <\/strong>\u6ce8\u89e3\u7c7b\uff0c\u5e76\u5b9e\u73b0 IProductService <\/strong>\u63a5\u53e3\u3002<\/span>
\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

\n ProductService.java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u6b65\u9aa414: <\/strong>\u5728
\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 index.html <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u521b\u5efa\u6240\u6709\u6587\u4ef6\u540e\uff0c\u9879\u76ee\u76ee\u5f55\u770b\u8d77\u6765\u50cf\u662f\u5728\u611a\u5f04:\n <\/div>\n

SpringBoot REST\u793a\u4f8b_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u8ba9\u6211\u4eec\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u3002\n <\/div>\n
\n \u6b65\u9aa415: <\/strong>\u6253\u5f00
\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
\n SpringBootRestExampleApplication.java <\/strong>\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u5f53\u5e94\u7528\u7a0b\u5e8f\u6210\u529f\u8fd0\u884c\u65f6\uff0c\u5b83\u5c06\u663e\u793a\u6d88\u606f\uff0c\u5982\u4e0b\u6240\u793a\n <\/div>\n

SpringBoot REST\u793a\u4f8b_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u6b65\u9aa416: <\/strong>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8c03\u7528 URL http:\/\/localhost:8080\/index.html\u3002\u5b83\u663e\u793a\u4e86
\n \u83b7\u53d6\u6240\u6709\u4ea7\u54c1<\/strong>\u7684\u94fe\u63a5\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002\n <\/div>\n

SpringBoot REST\u793a\u4f8b_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u5355\u51fb\u94fe\u63a5
\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

SpringBoot REST\u793a\u4f8b_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n


\n

<\/body>
\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}]}}