通过邮递员

时间:2018-10-23 18:31:09

标签: json spring postman

  

创建了Spring RestAPI Maven项目,将具有特定记录的患者添加到数据库中。借助于tokenNumber(tokenNumber是Primarykey,数据库中的自动增量)必须使用RestAPI通过PostMan(发送json对象)设置密码。

     

患者(实体类)

@Entity
@Table(name = "Registration")
public class Patient {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int tokenNumber;
    private String firstName;
    private String lastName;
    private Date dateOfBirth;
    private String gender;
    private String emailId;
    private String mobile;
    private String password;

    public String getPassword() {
        return password;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getTokenNumber() {
        return tokenNumber;
    }

    public void setTokenNumber(int tokenNumber) {
        this.tokenNumber = tokenNumber;
    }

    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 Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    // Constructor
    public Patient() {

    }
  

用于设置特定用户密码的DAO层代码:

     

PatientDAOImpl类(实现接口)

@Repository("patientDAO")
@Transactional
//@Repository
public class PatientDAOImpl implements PatientDAO {

    @Autowired
    SessionFactory sessionFactory;

    public PatientDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

@Override
    public boolean setPassword(int tokenNumber) {

            Patient patient=new Patient();
        patient.setTokenNumber(tokenNumber);
        Session session = sessionFactory.openSession();
        this.sessionFactory.getCurrentSession().saveOrUpdate(patient);
        Query query = session.createQuery("Update  Patient set Password=? where tokenNumber=" + tokenNumber);
        return false;
    }

}
  

控制器代码(它将接受来自PostMan的值)

@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

@PostMapping(value="/setpassword")
    public ResponseEntity<Patient> setPassword(@RequestBody Patient patient)
    {

        if(patientService.setPassword(patient.getTokenNumber()))
        {
        return new ResponseEntity<Patient>(HttpStatus.OK);
        }
        else
        {
            return new ResponseEntity<Patient>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
  

使用Json通过邮递员将该值发送到控制器   对象(在特定网址的帮助下)

{
"tokenNumber":9,
"password":"12345"
}
  按下邮递员令牌中的发送按钮后立即

  应该在数据库中更新12345。但在此守则中,所有价值   按下“发送”按钮后将为Null。此代码中有什么错误。

0 个答案:

没有答案