当我发帖时,即使我声明了@RequestMapping,它也显示了在Spring Boot中找不到404错误

问题描述 投票:0回答:1

我创建了一个用户服务,我想在其中添加新用户,并通过ID和其他属性来查找用户。自从了解到已经有功能以来,我已经扩展了JPA存储库。但是我什至无法保存新用户。

这是我的模特

package bt.gov.dit.userservice.model;

import javax.persistence.*;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long Id;

    @Column
    private String name;

    @Column
    private String email;

    @Column
    private String role;

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public User(Long id, String name, String email, String role) {
        Id = id;
        this.name = name;
        this.email = email;
        this.role = role;
    }

    public User() {
    }
}

这是我的存储库

 package bt.gov.dit.userservice.dao;

import bt.gov.dit.userservice.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User,Long> {

    User findUserByRole(String role);

}

这是我的服务

package bt.gov.dit.userservice.service;
import bt.gov.dit.userservice.dao.UserRepository;
import bt.gov.dit.userservice.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User save(User user){

        return userRepository.save(user);
    }
 public User getByRole(String role){
        return userRepository.findUserByRole(role);
    }
}

这是我的控制器

package bt.gov.dit.userservice.controller;

import bt.gov.dit.userservice.model.User;
//import bt.gov.dit.userservice.service.UserService;
import bt.gov.dit.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api")

public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public ResponseEntity<User> create(@Valid @RequestBody User user) {

        User updated = userService.create(user);
        return new ResponseEntity<User>(updated, new HttpHeaders(), HttpStatus.OK);

    }

    @GetMapping("/{role_id}")
    public ResponseEntity<User> getUserByRole(@PathVariable("role_id") String role)
    {
        User entity = userService.getByRole(role);

        return new ResponseEntity<User>(entity, new HttpHeaders(), HttpStatus.OK);
    }
}

我刚刚开始学习Spring boot,但似乎无法理解代码中的错误。我想插入/保存新用户。但是当我呼叫/ api / user时,它说未找到404。我正在使用H2内存数据库。

spring-boot spring-mvc controller spring-data-jpa spring-data-rest
1个回答
0
投票

您需要像这样映射请求的另一部分:

@@ Mapping(value =“ / user”,使用= MediaType.APPLICATION_JSON_UTF8_VALUE)

我也建议您使用带有@ EnableSwagger2注释的Springfox Swagger UI这将为您提供一个用户界面,其中包含http://localhost:8080/swagger-ui.html

上的所有请求

链接:https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

© www.soinside.com 2019 - 2024. All rights reserved.