Client tarafında üretilen rest yada controller'ı tüketmek için RestTemplate'yi kullanılır.

RestTemplate

Dto class'ları backend tarafındaki gibi aynıdır.

ProductDto.java


package com.springapp.springclient.business.dto;
 
public class ProductDto {
 
    private long productId;
    private String productName;
    private double salesPrice;
 
    public ProductDto() {
    }
 
    public ProductDto(long productId, String productName, double salesPrice) {
        this.productId = productId;
        this.productName = productName;
        this.salesPrice = salesPrice;
    }
 
    public long getProductId() {
        return productId;
    }
 
    public void setProductId(long productId) {
        this.productId = productId;
    }
 
    public String getProductName() {
        return productName;
    }
 
    public void setProductName(String productName) {
        this.productName = productName;
    }
 
    public double getSalesPrice() {
        return salesPrice;
    }
 
    public void setSalesPrice(double salesPrice) {
        this.salesPrice = salesPrice;
    }
 
}


ProductService.java ve ProductServiceImpl.java


public interface ProductService {
    
    public void save(ProductDto productDto);
    public ProductDto findById(Long productId);
    public List<ProductDto> findAll();
    public void deleteById(long productId);
}


package com.springapp.springclient.business.service.impl;
 
import com.springapp.springclient.business.dto.ProductDto;
import com.springapp.springclient.business.service.ProductService;
import com.springapp.springclient.data.client.ProductClient;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
/**
 *
 * @author kerem
 */
@Service
public class ProductServiceImpl implements ProductService {
 
    @Autowired
    private ProductClient productClient;
 
    @Override
    public void save(ProductDto productDto) {
        if (productDto.getProductId() == 0) {
            productClient.saveProduct(productDto);
        } else {
            productClient.putProduct(productDto);
        }
    }
 
    @Override
    public ProductDto findById(Long productId) {
        ProductDto productDto = null;
        if (productId != null){
            productDto = productClient.getProductDto(productId);
        }else {
            productDto = new ProductDto(0,"",0.0);
        }
        return productDto;
    }
 
    @Override
    public List<ProductDto> findAll() {
        return productClient.getProducts();
    }
 
    @Override
    public void deleteById(long productId) {
        productClient.deleteProduct(productId);
    }
 
}


Burada artık ProductClient var nasıl ki backend tarafında ProductRepository varsa front tarafta da bunu tüketecek ProductClient.java interface'si oluşturulur ve bu implement edilir.


ProductClient.java

package com.springapp.springclient.data.client;
 
import com.springapp.springclient.business.dto.ProductDto;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
 
public interface ProductClient {
 
    ProductDto getProductDto(@PathVariable("id") Long id);
 
    List<ProductDto> getProducts();
 
    void saveProduct(@RequestBody ProductDto productDto);
 
    void deleteProduct(@PathVariable("id") Long productDtoId);
 
    void putProduct(@RequestBody ProductDto productDto);
}



ProductClientImpl.java


package com.springapp.springclient.data.client.impl;
 
import com.springapp.springclient.business.dto.ProductDto;
import com.springapp.springclient.data.client.ProductClient;
import java.util.List;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;
 
@Component
public class ProductClientImpl implements ProductClient{
 
    public ProductDto getProductDto(@PathVariable("id") Long id) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ProductDto> productDtoEntity = restTemplate.exchange("http://localhost:8080/api/product/"+id,HttpMethod.GET,HttpEntity.EMPTY,ProductDto.class);
        return productDtoEntity.getBody();
    }
 
    public List<ProductDto> getProducts() {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<List<ProductDto>> productDtoListEntity = restTemplate.exchange
        (" http://localhost:8080/api/products", HttpMethod.GET,HttpEntity.EMPTY,new ParameterizedTypeReference<List<ProductDto>>(){});
        return productDtoListEntity.getBody();
    }
 
    public void saveProduct(@RequestBody ProductDto productDto) {
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity httpEntity = new HttpEntity(productDto);
        restTemplate.exchange("http://localhost:8080/api/product",HttpMethod.POST,httpEntity,Void.class);
    }
 
    public void putProduct(@RequestBody ProductDto productDto) {
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity httpEntity = new HttpEntity(productDto);
        restTemplate.exchange("http://localhost:8080/api/product",HttpMethod.PUT,httpEntity,Void.class);
    }
 
    public void deleteProduct(@PathVariable("id") Long productDtoId) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.exchange("http://localhost:8080/api/product", HttpMethod.DELETE,HttpEntity.EMPTY,Void.class);
    }
}



Son olarak da MVC tarafındaki classımız hazırlanır.


package com.springapp.springclient.presentation.mvc;
 
import com.springapp.springclient.business.dto.ProductDto;
import com.springapp.springclient.business.service.ProductService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
@Controller
public class ProductController {
 
 
    @Autowired
    private ProductService productService;
 
    @GetMapping(path = {"/product/edit", "/product/edit/{id}"})
    public String editProduct(Model model, @PathVariable(value = "id", required = false) Long id) {
        ProductDto productDto = productService.findById(id);
        model.addAttribute("product", productDto);
        return "/inventory/ProductEdit";
    }
 
    @PostMapping(path ={"/product/edit", "/product/edit/{id}"})
    public String editProduct(@Valid @ModelAttribute("product") ProductDto productDto, BindingResult bindingResult, Model model,
            @PathVariable(value = "id", required = false) Long id) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("errors", bindingResult);
            return "/inventory/ProductEdit";
        }
        model.addAttribute("message", "başarılı bir şekilde saved");
        productService.save(productDto);
        return "/inventory/ProductSuccess";
    }
 
    @GetMapping("/product/list")
    public String getProducts(Model model) {
        model.addAttribute("products", productService.findAll());
        return "/inventory/ProductList";
    }
 
    @GetMapping("/product/delete/{id}")
    public String deleteProduct(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        String message = "";
        if (id == null || id == 0) {
            message = "Lütfen geçerli bir id giriniz";
            redirectAttributes.addFlashAttribute("message", message);
            return "redirect:/product/list";
        }
        message = "Başarılı bir şekilde silindi";
        redirectAttributes.addFlashAttribute("message", message);
        productService.deleteById(id);
        return "redirect:/product/list";
    }
 
}