java 404错误来了,而击中一个职位的要求在 Postman

ycggw6v2  于 2023-04-19  发布在  Java
关注(0)|答案(3)|浏览(112)

URL:http://localhost:8098/Users/
身体

{
    "firstName": "sakti",
    "lastName": "behera",
    "email": "sakti.behera@gmail.com",
    "departmentId": "1"
}

控制器

@RestController
@RequestMapping("/Users")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @PostMapping("/")
    public User saveUser(@RequestBody User user) {
        return userService.saveUser(user);
        
    }
    @GetMapping("/{id}")
    public ResponseTemplateVO getUserWithDepartment(@PathVariable("id")  Long userId ) {
        return userService.getUserWithDepartment(userId);
        
    }

}

实体类

@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long userId;
    private String firstName;
    private String lastName;
    private String email;
    private Long departmentId;
    
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Long getDepartmentId() {
        return departmentId;
    }
    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }
    

}

售后服务:

enter code here
    @Service
    public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RestTemplate restTemplate;

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public ResponseTemplateVO getUserWithDepartment(Long userId) {
        ResponseTemplateVO vo = new ResponseTemplateVO();
        User user = userRepository.findByUserId(userId);
        Department department =
 restTemplate.getForObject("http://localhost:8097/departments/"+user.getD epartmentId(),Department.class);
        vo.setUser(user);
        vo.setDepartment(department);
        return vo;
    }

我点击postmethod,但得到404没有找到我已经添加了我的服务,实体,和控制器也.因为你给的建议添加(/保存),我尝试仍然它是不工作后,打了几次,我看到我的Eclipse控制台的东西,post方法是不允许的.所以它有什么做的代码或 Postman ?

bz4sfanl

bz4sfanl1#

您共享的代码看起来正确。
您是否记得使用端点使用post请求?下面是一个示例:

curl --location --request POST 'http://localhost:8098/users/' \

--header 'Content-Type: application/json' \

--data-raw '{

    "firstName": "sakti",

    "lastName": "behera",

    "email": "sakti.behera@gmail.com",

    "departmentId": "1"

}'
rur96b6h

rur96b6h2#

URL还应该包括您的服务名称。

http://localhost:8098/{service-name}/Users/
ar7v8xwq

ar7v8xwq3#

1.检查您的端口号是否为8098。
1.如果配置了项目路径,则URL应为http://localhost:8098/{project-path}/Users/
1.检查你的UserController是否在springboot扫描路径中。如果没有,你可以使用@ComponentScan annotation将其添加到扫描路径中。
1.您可以尝试将'/'更改为'/save'。URL为http://localhost:8098/Users/save
1.请给予你的 Postman 反馈。

相关问题