简体   繁体   中英

Getting error while running Spring boot app via Postman

While running the code getting whitelabel page error as shown in Postman Kindly help with what is going wrong: Note: All classes are below main class as per few checked solution. Issue remain same with @ComonentScan too.

Postman error: { "timestamp": "2022-08-05T12:08:42.938+00:00", "status": 404, "error": "Not Found", "path": "/dept/" }

Json given: { "deptName":"XYZ", "address": "XYZ" }

Code:

Entity:
package com.microservice.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
//import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Entity
@Data

@AllArgsConstructor
@NoArgsConstructor
public class Department {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int departmentId;
    

    private String deptName;


    private String address;

}

Service:

package com.microservice.Service;

import com.microservice.Entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.microservice.Repository.DepartmentRepository;

@Service

    public class DepartmentService {
        @Autowired
        private DepartmentRepository departmentRepository;
    
    
    
        public Department getByID(int departmentId)
        {
            return departmentRepository.findByDepartmentId(departmentId);
        }
    
        public Department saveDepartment(Department department) {
            return departmentRepository.save(department);
        }
    }

Controller:

package com.microservice.FController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.microservice.Entity.Department;
import com.microservice.Service.DepartmentService;

@RestController
@RequestMapping("/dept")
public class DepartmentController {

  @Autowired
    private DepartmentService departmentservice;




 @PostMapping("/")
    public Department saveDepartment(@RequestBody Department department){
        return departmentservice.saveDepartment(department);
    }

@GetMapping("/{id}")
    public Department getByID(@PathVariable("id") Integer departmentId  ) {
    System.out.println("Inside get id controller method");

    return departmentservice.getByID(departmentId);
}



}

Repository:

package com.microservice.Repository;
import org.springframework.data.jpa.repository.JpaRepository;

import com.microservice.Entity.Department;

public interface DepartmentRepository extends JpaRepository<Department,Integer> {

     Department findByDepartmentId(int departmentId);
}

Main Class:
package com.microservice.Departmentservice;

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

@SpringBootApplication
public class DepartmentServiceApplication {

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

}

I've tried your code and seams to be correct. Just two check you must done this the URL that you request in Postman :

  • First : Make sure that you make a POST request to the URL not GET
  • Second : You add a slash character / in @PostMapping("/") on top of saveDepartment method. So, you have to make your post request as:
 http://localhost:9009/dept/

Notice the trailing slash at the end of the request.

Otherwise you get 404 not found error if you missed this character /


Another way is just simply remove / from @PostMapping("") on top of saveDepartment method:

@PostMapping("")
public Department saveDepartment(@RequestBody Department department){
    return departmentservice.saveDepartment(department);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM