从请求后java获取变量

从请求后java获取变量,java,Java,我想创建一个通过API的其余部分发送消息的微服务。如果通过POST请求传输数据,我希望立即使用此数据发送消息。电话号码和其他数据目前是硬编码的。 如何在POST请求中直接使用数据 @PostMapping("/contacts") public ResponseEntity<Object> createContact (@RequestBody Contact contact) { String phone_number="xxxxxxxxxxx"

我想创建一个通过API的其余部分发送消息的微服务。如果通过POST请求传输数据,我希望立即使用此数据发送消息。电话号码和其他数据目前是硬编码的。 如何在POST请求中直接使用数据


@PostMapping("/contacts")
    public ResponseEntity<Object> createContact (@RequestBody Contact contact)
    {

        String phone_number="xxxxxxxxxxx";
        String customer_phone_number="xxxxxxxxxx";

        open(phone_number,customer_phone_number,custom_uid);
        Contact savedContact = contactRepository.save(contact);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
        .path("/{userName}").buildAndExpand(savedContact.getUserName()).toUri();
        return ResponseEntity.created(location).build();

}


@后映射(“/contacts”)
public ResponseEntity createContact(@RequestBody Contact)
{
字符串电话号码=“XXXXXXXXXX”;
字符串customer\u phone\u number=“xxxxxxxxx”;
打开(电话号码、客户电话号码、自定义uid);
Contact savedContact=contactRepository.save(Contact);
URI位置=ServletUriComponentsBuilder.fromCurrentRequest()
.path(“/{userName}”).buildAndExpand(savedContact.getUserName()).toUri();
返回ResponseEntity.created(location.build();
}
有人能帮我吗



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

        private String userName;
        private String phone_number;
        private String customer_phone_number;
        private String template;

        public int getContact_id() {
            return contact_id;
        }
        public void setContact_id(int contact_id) {
            this.contact_id = contact_id;
        }
        public  String getPhone_number() {
            return phone_number;
        }
        public void setPhone_number(String phone_number) {
            this.phone_number = phone_number;
        }
        public String getCustomer_phone_number() {
            return customer_phone_number;
        }
        public void setCustomer_phone_number(String customer_phone_number) {
            this.customer_phone_number = customer_phone_number;
        }

        public String getTemplate() {
            return template;
        }
        public void setTemplate(String template) {
            this.template = template;
        }

        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public Contact()
        {

        }
        public Contact(int contact_id,String userName, String phone_number, String customer_phone_number,  String template) {
            super();

            this.contact_id = contact_id;
            this.userName=userName;
            this.phone_number = phone_number;
            this.customer_phone_number = customer_phone_number;

            this.template = template;
        }



这是我的getters联系人类

您的联系人类中有什么?我相信你应该把你的联系信息放在那里,然后用getter把它们取出来。你不是可以通过
contact
实例获得这些数据吗?它是
@RequestBody
的一个实例。所以当我通过get-Request查询数据时,我也会把数据取出来。但是,如果我想在post请求中立即查询数据,则不会传输数据。我假设它是这样工作的:
public ResponseEntity createContact(@RequestBody Contact,HttpServletRequest,HttpServletResponse response)抛出ServletException,IOException{String phone_number=request.getParameter(“phone_number”);