Home > other >  Controller Method is not getting triggered
Controller Method is not getting triggered

Time:12-27

I have recently started exploring Spring boot. I am following https://www.bezkoder.com/spring-boot-jdbctemplate-postgresql-example/ documentation.

I have created all files as instructed in the documentation.

Here goes my code:

******AppApplication.java

`package com.triveni.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

public class AppApplication {

   public static void main(String[] args) {
      SpringApplication.run(AppApplication.class, args);
   }

}`

***** IProductRepository *****

`package com.triveni.repository;

import com.triveni.models.ProductModel;

import java.util.List;

public interface IProductRepository {
    int create(ProductModel productModel);
    int update (ProductModel productModel);
    ProductModel findById(int id);
    List<ProductModel> findAll();

    List<ProductModel> findByActive(boolean active);
}`

***** ProductModel.java *****

package com.triveni.models;

public class ProductModel {
    private int productId;
    private String productName;
    private int cost;
    private Boolean active;
    private int descriptionId;



    public ProductModel(int productId, String productName, int cost, Boolean active, int descriptionId){
        this.productId = productId;
        this.productName = productName;
        this.cost = cost;
        this.active = active;
        this.descriptionId = descriptionId;
    }

    public void setProductId(int productId){
        this.productId = productId;
    }
    public long getProductId(){
        return productId;
    }

    public void setProductName(String productName){
        this.productName = productName;
    }

    public String getProductName(){
        return productName;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }
    public int getCost() {
        return cost;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }
    public Boolean getActive() {
        return active;
    }

    public void setDescriptionId(int descriptionId) {
        this.descriptionId = descriptionId;
    }

    public int getDescriptionId() {
        return descriptionId;
    }


}

*** Product Repository *****

package com.triveni.data;

import com.triveni.models.ProductModel;
import com.triveni.repository.IProductRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class ProductRepository implements IProductRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int create(ProductModel productModel) {
        return jdbcTemplate.update("INSERT INTO public.product(\n"  
                "\t\"productId\", \"productName\", cost, \"DescriptionId\", active)\n"  
                "\tVALUES (?, ?, ?, ?, ?);",new Object[]{productModel.getProductId(),productModel.getProductName(),
                productModel.getCost(), productModel.getDescriptionId(),productModel.getActive()});
    }

    @Override
    public int update(ProductModel productModel) {
        return 0;
    }

    @Override
    public ProductModel findById(int id) {
        return null;
    }

    @Override
    public List<ProductModel> findAll() {
        return jdbcTemplate.query("SELECT \"productId\", \"productName\", cost, \"DescriptionId\", active\n"  
                "\tFROM public.product",BeanPropertyRowMapper.newInstance(ProductModel.class));
    }

    @Override
    public List<ProductModel> findByActive(boolean active) {
        return null;
    }
}

***** ProductController.java *****

package com.triveni.controllers;

import java.util.ArrayList;
import java.util.List;

import com.triveni.data.ProductRepository;
import com.triveni.models.ProductModel;
import com.triveni.repository.IProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1")
public class ProductController {

    @Autowired
    ProductRepository productRepository;

   @GetMapping("/product")
    public ResponseEntity<List<ProductModel>> getAllProducts(){
       try{
        List<ProductModel> products = new ArrayList<ProductModel>();
           productRepository.findAll().forEach(products::add);;
           return new ResponseEntity<>(products, HttpStatus.OK);
       }catch (Exception e){
           return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);

       }
   }

}

My Project Folder Structure

I am getting following error

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Dec 27 13:51:39 IST 2022 There was an unexpected error (type=Not Found, status=404).

Any help will be highly appreciated.

CodePudding user response:

try to add this to annotation @CrossOrigin("*") after @RestController.

CodePudding user response:

I believe you are just launching the spring application and on launch, this error shows up. In order to test your URL you can type <YOUR-URL>/app/v1/product This should return you the Response you are looking for.

  • Related