无法得到适当的GET响应(Unable to get proper GET response)

2019-10-29 23:40发布

我想实现一个GET和春季启动POST方法。 当调用GET方法没有得到适当的反应。

我的控制器类

@RestController
@RequestMapping("api/public")
public class PublicRestApiController {

@GetMapping("users")
public List<User> users(){
    return this.userRepository.findAll();
}

@PostMapping(path="/save")
@ResponseBody public User insertusers( @RequestBody UserRequest requestUser){
    User user = new User(requestUser.getUsername(),passwordEncoder.encode(requestUser.getPassword()),requestUser.getRoles(),requestUser.getPermissions());
    try { 
        user = this.userRepository.save(user);
        return user;
    }
    catch(Error e) {
        return user;
    }
}

}

UserRequest类

public class UserRequest {

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public int getActive() {
    return active;
}
public void setActive(int active) {
    this.active = active;
}
public String getRoles() {
    return roles;
}
public void setRoles(String roles) {
    this.roles = roles;
}
public String getPermissions() {
    return permissions;
}
public void setPermissions(String permissions) {
    this.permissions = permissions;
}
private String username;
private String password;
private int active;
private String roles = "";
private String permissions = "";

}

用户类别

@Entity
@Table(name = "USER_DETAILS")
public class User {

@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(nullable = false)
private String username;

@Column(nullable = false)
private String PASSWORD;

@Column
private int ACTIVE;

@Column
private String ROLES = "";

@Column
private String PERMISSIONS = "";

public User(String username, String password, String roles, String permissions){
    this.username = username;
    this.PASSWORD = password;
    this.ROLES = roles;
    this.PERMISSIONS = permissions;
    this.ACTIVE = 1;
}

public User(){}

public int getId() {
    return id;
}

public String getUsername() {
    return username;
}

public String getPassword() {
    return PASSWORD;
}

public int getActive() {
    return ACTIVE;
}

public String getRoles() {
    return ROLES;
}

public String getPermissions() {
    return PERMISSIONS;
}

public void setId(int id) {
    this.id = id;
}

public void setUsername(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.PASSWORD = password;
}

public void setActive(int active) {
    this.ACTIVE = active;
}

public void setRoles(String roles) {
    this.ROLES = roles;
}

public void setPermissions(String permissions) {
    this.PERMISSIONS = permissions;
}
}

UserRepository类

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

}

POST方法工作正常,但在调用get方法,id字段来了默认值0.Not获取正确的ID值。 而且还同时运行的服务器,如果我叫POST方法,它是在数据库中插入数据,而是使得新插入的数据不会被GET method.Only这些数据变得越来越牵强,我已经插入到数据库中获得牵强。

这是在数据库中的数据

这是我得到的形式GET方法响应

Answer 1:

您的用户类与类主键int和在你的仓库,你已经扩展了长用户类,它应该是整数,如下

库:

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

 }

更新:

 @Autowired UserRepository userRepository;

         @GetMapping("users")
         public List<User> users(){
              return userRepository.getAllUsers();
         }

库:

 @Repository
 public interface UserRepository extends JpaRepository<User, Integer>{
      @Query(value="select * from USER_DETAILS",nativeQuery=true)
      List<User> getAllUsers();
 }

进口@Queryimport org.springframework.data.jpa.repository.Query;


UPDATE2:项目结构IDEL方式是根据类的行为有不同的套餐。

  • 调节器
  • 服务
  • 知识库
  • 模型或实体
  • 的DTO,DAO的
  • 安全,配置等按该requirnments

     //Controller
     @yourMapping
     public returnType method1()
     {
         myService.performBusinessLoginonMethod1(...) //if you are using services
         myReposiroty.DBOpertaion(...) //as per above case
     }

     //Service: myService
     returnType  performBusinessLoginonMethod1(...) 
     {
         myRepository.DBOpertaion(...)
     }

     //Repository: myRepository     
     @Modifying // import org.springframework.data.jpa.repository.Modifying;
     @Query(...)
     returnType DBOpertaion(...)


Answer 2:

你是不是正确的返回效应初探。 尝试这个。

    @GetMapping("users")
public ResponseEntity<List<Article>> users(){
        return new ResponseEntity<List<Article>>(this.userRepository.findAll(), HttpStatus.OK);
}


文章来源: Unable to get proper GET response