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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| package com.example.faststart;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.Optional;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.core.annotation.Order; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;
import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@SpringBootApplication public class FaststartApplication {
public static void main(String[] args) { SpringApplication.run(FaststartApplication.class, args); }
@Bean @Order(0) CommandLineRunner saveSomeRepositories(ProductRepository productRepository) { return args -> { productRepository.save(new Product(null, "Bob", 12.1, true)); productRepository.save(new Product(null, "Stevie", 10.1, true)); productRepository.findAll().forEach(product -> { System.out.println(product.toString()); }); }; }
@Bean @Order(1) CommandLineRunner run(ProductRepository productRepository) { return args -> { productRepository.findAll().forEach(product -> { System.out.println(product.getId()); }); }; } }
@Entity @Data @NoArgsConstructor @AllArgsConstructor
@Table(name = "mytest") class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Double price; private Boolean isInStock; }
interface ProductRepository extends JpaRepository<Product, Long>{ };
@RestController @AllArgsConstructor class ProductController { private final ProductRepository productRepository;
@GetMapping(path = "/products") public List<Product> list() { return productRepository.findAll(); }
@GetMapping(path = "/products/{id}") public Product getProduct(@PathVariable Long id) { return productRepository.findById(id).get(); }
@PostMapping(path = "/products") public Product postProduct(@RequestBody Product product) { return productRepository.save(product); }
@PutMapping(path = "/products/{id}") public Product putProduct(@PathVariable Long id, @RequestBody Product product) { product.setId(id); return productRepository.save(product); }
@PatchMapping(path = "/products/{id}") public Product patchProduct(@PathVariable Long id, @RequestBody Product product) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Optional<Product> originProductOpt = productRepository.findById(id); if (originProductOpt.isEmpty()) { return new Product(); } Product originProduct = originProductOpt.get(); Class<?> productClass = product.getClass(); Method[] methods = productClass.getMethods(); for (Method method: methods) { String methodName = method.getName(); Class<?> methodReturnedType = method.getReturnType(); if (methodName.startsWith("get") && !"getClass".equals(methodName)) { Object value = method.invoke(product); if (null == value) { continue; }
String setMethodName = methodName.replaceAll("get", "set"); if (methodReturnedType == Integer.class) { Integer newValue = (Integer)value; Method setMethod = productClass.getMethod(setMethodName, Integer.class); setMethod.invoke(originProduct, newValue); continue; } if (methodReturnedType == Long.class) { Long newValue = (Long)value; Method setMethod = productClass.getMethod(setMethodName, Long.class); setMethod.invoke(originProduct, newValue); continue; } if (methodReturnedType == Double.class) { Double newValue = (Double)value; Method setMethod = productClass.getMethod(setMethodName, Double.class); setMethod.invoke(originProduct, newValue); continue; } if (methodReturnedType == Boolean.class) { Boolean newValue = (Boolean)value; Method setMethod = productClass.getMethod(setMethodName, Boolean.class); setMethod.invoke(originProduct, newValue); continue; }
String newValue = (String)value; Method setMethod = productClass.getMethod(setMethodName, String.class); setMethod.invoke(originProduct, newValue); } }
return productRepository.save(originProduct); }
@DeleteMapping(path = "/products/{id}")server.port=8085 public void deleteProduct(@PathVariable Long id) { productRepository.deleteById(id); } }
|
v1.5.2