SpringBoot中通过@Service声明服务组件类,这些类与@RestController类隔离并在不同的层用来处理业务逻辑,创建一个服务类组件如下:
1 | public initerface ProductService(){ |
1 |
|
在本教程中,使用到Product Service API(s)来存储,检索,更新以及删除数据,之前我们的业务逻辑是放在@RestController类中,现在我们将业务逻辑代码从控制器代码中转移到服务组件。
首先你需要创建一个接口约束add,edit,get以及delete方法,如下:1
2
3
4
5
6
7
8
9
10
11
12package com.restfulapio.demo.service;
import com.restfulapio.demo.bean.Product;
import java.util.Collection;
public interface ProductService {
public abstract void createProduct(Product product);
public abstract void updateProduct(String id, Product product);
public abstract void deleteProduct(String id);
public abstract Collection<Product> getProducts();
}
然后你应该创建一个实现这个接口的类并标注@Service注解,然后实现store,retrieve,delete和update的具体业务逻辑,如下;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41package com.restfulapio.demo.service;
import com.restfulapio.demo.bean.Product;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class ProductServiceImpl implements ProductService {
private static Map<String, Product> productMap = new HashMap<>();
static{
Product honey = new Product();
honey.setId("1");
honey.setName("Honey");
productMap.put(honey.getId(), honey);
Product almond = new Product();
almond.setId("2");
almond.setName("Almond");
productMap.put(almond.getId(), almond);
}
public void createProduct(Product product) {
productMap.put(product.getId(), product);
}
public void updateProduct(String id, Product product) {
productMap.remove(id);
product.setId(id);
productMap.put(id, product);
}
public void deleteProduct(String id) {
productMap.remove(id);
}
public Collection<Product> getProducts() {
return productMap.values();
}
}
最后,我们只需要在@RestController控制器类中自动注入Service服务类组件就可以了,如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.restfulapio.demo.controller;
import com.restfulapio.demo.bean.Product;
import com.restfulapio.demo.exception.ProductException;
import com.restfulapio.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
public class ProductServiceController {
ProductService productService;
(value = "/products")
public ResponseEntity<Object> getProduct(){
return new ResponseEntity<Object>(productService.getProducts(), HttpStatus.OK);
}
(value = "/products",method= RequestMethod.POST)
public ResponseEntity<Object> createProduct(@RequestBody Product product){
productService.createProduct(product);
return new ResponseEntity<>("Product is created successfully",HttpStatus.CREATED);
}
(value = "/products/{id}",method = RequestMethod.PUT)
public ResponseEntity<Object> updateProduct(@PathVariable("id") String id,@RequestBody Product product){
productService.deleteProduct(id);
product.setId(id);
productService.updateProduct(id, product);
return new ResponseEntity<>("Product is updated successfully",HttpStatus.OK);
}
(value = "/products/{id}",method = RequestMethod.DELETE)
public ResponseEntity<Object> delete(@PathVariable("id") String id){
productService.deleteProduct(id);
return new ResponseEntity<>("Product is deleted successfully",HttpStatus.OK);
}
}
测试
GET API
POST API
UPDATE API
DELETE API
评论加载中