Java 将JSONObject object=newjsonobject(response.body())添加到MySQL DB表中,类似于MyResponses,带有类似于response(Text)的文本字段
我是Spring的初学者,找不到如何做到这一点的信息。我有MySQL表1)带有字段Id、FName、LName、DOB、国籍和已验证(tinyint(1))等的Customer。2)带有字段Id AI PK、Customer_Id、response(text)、date_created(datetime)的MyResponses。为此,我有一个实体类,如-Customer和我的所有字段(使用Lombok表示get set等)。我需要的是,在下面的代码中,我使用我的客户字段对API执行Http请求,这里有来自服务器的响应“JSONObject object=new JSONObject(response.body());”。我的对象检查这个响应,并为这个数据库字段Customer-verified(tinyint(1))返回true或false。及 问题是我该怎么说Java 将JSONObject object=newjsonobject(response.body())添加到MySQL DB表中,类似于MyResponses,带有类似于response(Text)的文本字段,java,mysql,spring,spring-boot,jdbctemplate,Java,Mysql,Spring,Spring Boot,Jdbctemplate,我是Spring的初学者,找不到如何做到这一点的信息。我有MySQL表1)带有字段Id、FName、LName、DOB、国籍和已验证(tinyint(1))等的Customer。2)带有字段Id AI PK、Customer_Id、response(text)、date_created(datetime)的MyResponses。为此,我有一个实体类,如-Customer和我的所有字段(使用Lombok表示get set等)。我需要的是,在下面的代码中,我使用我的客户字段对API执行Http请求
JSONObject object = new JSONObject(response.body());
到我的第二个MySQL DB表,上面有如下字段
- id) AI,
customer_id) my customer_id ,
response here my customer_response.body() and data_created) now;
对于DB连接,我使用@Autowired
private JdbcTemplate jdbcTemplate;
我需要向我的代码中添加什么来填充我上面共享的第二个表
public class MyService {
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
public static boolean sendPost() throws IOException, InterruptedException {
Customer postData = new Customer();
DateFormat df = new SimpleDateFormat(dateFormatddMMyyyy);
Date dob = postData.getDateOfBirth();
String dobAsString = df.format(dob);
String body = new StringBuilder()
.append("{")
.append("\"firstName\":")
.append("\"")
.append(postData.getFirstName())
.append("\"")
.append(",")
.append("\"middleName\":\"\",")
.append("\"lastName\":")
.append("\"")
.append(postData.getLastName())
.append("\"")
.append(",")
.append("\"originalName\":\"\",")
.append("\"gender\":\"\",")
.append("\"dob\":")
.append("\"")
.append(dobAsString)
.append("\"")
.append(",")
.append("\"country\":")
.append("\"")
.append(postData.getNationality())
.append("\"")
.append(",")
.append("\"matchRate\":\"50\",")
.append("\"maxResultCount\":\"100\"")
.append("}").toString();
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(body))
.uri(URI.create(ApiUrl))
.setHeader("api-key", ApiKey)
.header("Content-Type", "application/json")
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
JSONObject object = new JSONObject(response.body());
if (object.getInt("numberOfMatches") == 0)
{
return true;
}
else return false;
}
}
公共类MyService{
私有静态最终HttpClient HttpClient=HttpClient.newBuilder()
.version(HttpClient.version.HTTP_2)
.build();
公共静态布尔sendPost()引发IOException、InterruptedException{
客户postData=新客户();
DateFormat df=新的SimpleDataFormat(dateFormatddMMyyyy);
Date dob=postData.getDateOfBirth();
字符串dobAsString=df.format(dob);
字符串正文=新的StringBuilder()
.append(“{”)
.append(“\”firstName\”:”)
.append(“\”)
.append(postData.getFirstName())
.append(“\”)
.附加(“,”)
.append(“\”middleName\”:\“\”,”)
.append(“\”lastName\“:”)
.append(“\”)
.append(postData.getLastName())
.append(“\”)
.附加(“,”)
.append(“\”原始名称\“:\”,”)
.append(“\”性别\“:\”,”)
.append(“\”dob\“:”)
.append(“\”)
.append(dobAsString)
.append(“\”)
.附加(“,”)
.append(“\”国家\“:”)
.append(“\”)
.append(postData.getNationality())
.append(“\”)
.附加(“,”)
.append(“\”匹配率“:\”50\“,”)
.append(“\”maxResultCount\”:\“100\”)
.append(“}”).toString();
HttpRequest请求=HttpRequest.newBuilder()
.POST(HttpRequest.bodypublisher.ofString(body))
.uri(uri.create(apirl))
.setHeader(“api密钥”,ApiKey)
.header(“内容类型”、“应用程序/json”)
.build();
HttpResponse response=httpClient.send(请求,HttpResponse.BodyHandlers.ofString());
JSONObject对象=新的JSONObject(response.body());
if(object.getInt(“numberOfMatches”)==0
{
返回true;
}
否则返回false;
}
}
Welcome,要提高您的体验,请阅读,然后查看,以及如何创建