[英]Getting error while running Spring boot app via Postman
運行代碼時出現白標簽頁面錯誤,如 Postman 所示請幫助解決問題:注意:所有類都低於主要的 class 根據幾個檢查的解決方案。 @ComonentScan 的問題也一樣。
Postman 錯誤:{“時間戳”:“2022-08-05T12:08:42.938+00:00”,“狀態”:404,“錯誤”:“未找到”,“路徑”:“/dept/”}
Json 給定:{“部門名稱”:“XYZ”,“地址”:“XYZ”}
代碼:
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;
}
服務:
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);
}
}
存儲庫:
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);
}
}
我試過你的代碼和接縫是正確的。 只需兩次檢查,您必須在Postman中請求的URL中完成此操作:
POST
請求,而不是GET
saveDepartment
方法之上的@PostMapping("/")
中添加一個斜杠字符/
。 因此,您必須將您的發布請求設置為:http://localhost:9009/dept/
請注意請求末尾的斜杠。
否則,如果你錯過了這個字符,你會得到 404 not found 錯誤/
另一種方法是簡單地從saveDepartment
方法頂部的@PostMapping("")
中刪除/
:
@PostMapping("")
public Department saveDepartment(@RequestBody Department department){
return departmentservice.saveDepartment(department);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.