无法获得正确的GET响应 [英] Unable to get proper GET response

查看:103
本文介绍了无法获得正确的GET响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在spring-boot中实现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.无法获取正确的ID值.而且在运行服务器时,如果我调用POST方法,它是在数据库中插入数据,但是GET方法不会获取新插入的数据.只有那些数据已经被获取,而我已经将它们插入数据库中. >

这是数据库中的数据

这是我从GET方法获得的响应

解决方案

您的USER类具有类型为int的主键,并且在您的存储库中,您已经扩展了 具有long的USER类,应为Integer,如下所示

存储库:

 @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();
 }

import org.springframework.data.jpa.repository.Query;

导入@Query

UPDATE2: 项目结构的Idel方法是根据类的行为使用不同的包.

  • 控制器
  • 服务
  • 存储库
  • 模型或实体
  • DTO,DAO
  • 根据要求的安全性,配置等

     //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(...)

I am trying to implement a GET and a POST method in spring-boot. While calling GET method not getting proper response.

My Controller class

@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 class

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 = "";

}

User Class

@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 class

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

}

POST method is working fine, but while calling GET method, id field is coming default value 0.Not fetching proper ID values. And also while running server, if I call POST method, it is inserting data in database, but that newly inserted data is not getting fetched by GET method.Only those data are getting fetched, which I have already inserted in the database.

This is the data in database

This is the response which I am getting form GET method

解决方案

Your USER class has Primary key with Type int and in your Repository, you have extended USER class with long which should be Integer as below

Repository:

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

 }


UPDATE:

 @Autowired UserRepository userRepository;

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

Repository:

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

import @Query from import org.springframework.data.jpa.repository.Query;


UPDATE2: Idel way of project structure is to have different packages according to class behavior.

  • Controller
  • Service
  • Repository
  • Models or Entity
  • DTOs, DAOs
  • Security, Config etc as per the 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(...)

这篇关于无法获得正确的GET响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆