简体   繁体   English

通过 Postman 运行 Spring 启动应用程序时出错

[英]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.运行代码时出现白标签页面错误,如 Postman 所示请帮助解决问题:注意:所有类都低于主要的 class 根据几个检查的解决方案。 Issue remain same with @ComonentScan too. @ComonentScan 的问题也一样。

Postman error: { "timestamp": "2022-08-05T12:08:42.938+00:00", "status": 404, "error": "Not Found", "path": "/dept/" } Postman 错误:{“时间戳”:“2022-08-05T12:08:42.938+00:00”,“状态”:404,“错误”:“未找到”,“路径”:“/dept/”}

Json given: { "deptName":"XYZ", "address": "XYZ" } Json 给定:{“部门名称”:“XYZ”,“地址”:“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: 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 :只需两次检查,您必须在Postman中请求的URL中完成此操作:

  • First : Make sure that you make a POST request to the URL not GET首先:确保您向 URL 发出POST请求,而不是GET
  • Second : You add a slash character / in @PostMapping("/") on top of saveDepartment method.第二:在saveDepartment方法之上的@PostMapping("/")中添加一个斜杠字符/ 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 /否则,如果你错过了这个字符,你会得到 404 not found 错误/


Another way is just simply remove / from @PostMapping("") on top of saveDepartment method:另一种方法是简单地从saveDepartment方法顶部的@PostMapping("")中删除/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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