java 使用RESTFul Web服务实现POST方法在MYSQL数据库中插入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40986411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:35:24  来源:igfitidea点击:

Implementing POST Method to insert data in MYSQL database using RESTFul web service

javamysqlrestpostjersey

提问by Mohammad Hasan

I am trying to implement POST method using restful web service using jersey and Postman for rest client in java. Now I am unable to insert data into the database using post method. I have table in mysql database name person which has 3 fields. id(primary key,auto increment), firstname, lastname. I made there java classes one is person as model class and PersonResource class for declaring REST api and PersonService for implementing REST method to make crud operation.

我正在尝试使用 jersey 和 Postman 在 java 中的rest客户端使用restful web服务来实现POST方法。现在我无法使用 post 方法将数据插入到数据库中。我在有 3 个字段的 mysql 数据库名称 person 中有表。id(主键,自动递增),名字,姓氏。我在那里创建了一个 java 类,一个是作为模型类的人,另一个是用于声明 REST api 的 PersonResource 类和用于实现 REST 方法以进行 crud 操作的 PersonService。

   PersonResource class is:

        @Path("/person")
        public class PersonResource {
            DbConnection connect=new DbConnection();
            Connection conn=null;

            PersonService service=new PersonService();

            @POST 
            @Consumes(MediaType.APPLICATION_JSON)// specifies the request body content
            @Produces(MediaType.APPLICATION_JSON)
            public Person addMessage(Person person) throws Exception{
            return service.addMessage(person);
        }
        }   

PersonService class is:

PersonService 类是:

        public class PersonService {

            DbConnection connect=new DbConnection();
            Connection conn=null;

            public Person addMessage(Person person) throws Exception {

            Person p=new Person();
            String sql="insert into person (firstname,lastname) values(?,?)";
            conn=connect.databaseConnection();
            PreparedStatement pst=conn.prepareStatement(sql);
                pst.setString(1,p.getFirstName());
                pst.setString(2,p.getLastName());   
                pst.executeUpdate();
                pst.close();
                return person;
        }
        }

I have successfully retrieve person info by person id. Now i want to make post request from postman to insert data in the mysql database.

我已成功通过人员 ID 检索人员信息。现在我想从邮递员发出 post 请求以在 mysql 数据库中插入数据。

From postman i made post request and select the content-type application/json in the header and write the data in the raw text in json format.when i hit the send button it getting 415 Unsupported Media Type.

从邮递员那里,我提出了发布请求,并在标题中选择了内容类型应用程序/json,并以 json 格式在原始文本中写入数据。当我点击发送按钮时,它得到 415 Unsupported Media Type。

can anybody help me how to capture the postman data to insert into the database or any help how to implement post method to insert record in database using REST from postman? Here is my postman window. enter image description hereThanks in advance

任何人都可以帮助我如何捕获邮递员数据以插入到数据库中,或者任何帮助如何实现 post 方法以使用邮递员的 REST 在数据库中插入记录?这是我的邮递员窗口。 在此处输入图片说明提前致谢

回答by Nicolas Fontenele

Since you get an unsupported operation exception your service is probably there and you have been able to fetch it. However the data format is not working in between. This normally happen when you are expecting something different that was sent ( JSON ) which doesn't see the case since you declare to consume a JSON.

由于您收到不受支持的操作异常,您的服务可能存在并且您已经能够获取它。但是,数据格式在两者之间不起作用。这通常发生在您期望发送的不同内容 ( JSON ) 时,由于您声明使用 JSON 而没有看到这种情况。

My take is that serialization is not working and you are getting String and expecting JSON. For confirming that you can modify or create another service accepting plain text and sending the same request body.

我的看法是序列化不起作用,您正在获取 String 并期待 JSON。用于确认您可以修改或创建另一个接受纯文本并发送相同请求正文的服务。

If that is confirmed - jersey should be able to serialize easily simple objects. You just need to add the right dependencies for that. Whether is Hymanson, gson, etc. Normally you can simply declare jersey-json dependency like below. Make sure some serializer is present.

如果这一点得到确认 - jersey 应该能够轻松地序列化简单的对象。您只需要为此添加正确的依赖项。是否是Hymanson、gson 等。通常你可以简单地声明jersey-json 依赖项,如下所示。确保存在一些序列化程序。

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

check this tutorialtutorial: is pretty much what you are willing to do.

检查本教程教程:几乎是您愿意做的。