当我发布时,它显示了在spring-boot中找不到的错误404,即使我声明了@RequestMapping



我制作了一个用户服务,我想在其中添加新用户,通过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引导,似乎不明白代码中有什么错误。我想插入/保存新用户。但当我调用/api/user时,它显示404未找到。我在内存数据库中使用h2。

我认为问题在于控制器方法参数name-role->role_id

@GetMapping("/{role_id}")
public ResponseEntity<User> getUserByRole(@PathVariable("role_id") String role_id)
{
User entity = userService.getByRole(role_id);
return new ResponseEntity<User>(entity, new HttpHeaders(), HttpStatus.OK);
}

rol_id应该与您在路由中提到的相同。路由示例->localhost:8080/api/2

您需要映射请求的其他部分,如下所示:

@PostMapping(value="/user",consumps=MediaType.APPLICATION_JSON_UTF8_value(

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

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

最新更新