我试图从JDBC返回一个值列表,但是为了解决这个问题,数据库中有多个列,我只是创建了一个JSON对象来创建这样的对象
{
"Results 1": {
"IP": "192.168.1.2",
"Port": "13442",
"Domain": "google.com"
},
"Results 2": {
"IP": "192.168.1.2",
"Port": "13442",
"Domain": "google.com"
}
}问题是我得到了这个错误WARN 43953 --- [nio-1900-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
我试过的
我试过使用散列映射,但它不起作用,我甚至不知道它是否能像希望的那样返回值。
我所希望的
我希望在最后我能得到一个类似于我上面显示的方式的值列表,所有这些都是分开的。这将稍后显示在HTML上,所以如果我正在做什么,我也不应该让我知道
这是我的密码
package com.mchugo.que.McHugoQue.Controller;
import com.mchugo.que.McHugoQue.Models.ConnectionDetails;
import com.mchugo.que.McHugoQue.Models.SearchCredential;
import com.mysql.cj.xdevapi.JsonArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
@RestController
public class ApiController {
@RequestMapping(value={"/search/credential", "xyz"}, method={RequestMethod.POST,RequestMethod.PUT, RequestMethod.GET})
public JsonArray searchCredential(@RequestBody SearchCredential searchCredential){
Connection connection = null;
Statement st = null;
ResultSet rs = null;
String Username = searchCredential.getUsername();
//grabs connection details
ConnectionDetails connectionDetails = new ConnectionDetails();
String username = "";
String password = "";
JsonArray array = new JsonArray();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://" + connectionDetails.getHost() + "/" + connectionDetails.getDatabase(), connectionDetails.getUsername(), connectionDetails.getPassword());
Statement statement = con.createStatement();
ResultSet res = statement.executeQuery("SELECT * FROM credentials WHERE identifier = '" + Username + "'");
while(res.next()){
JSONObject data = new JSONObject();
username = res.getString("identifier");
password = res.getString("password");
data.put("Username", username);
data.put("Password", password);
array.add(data);
}
}
catch(Exception ex){
System.out.println("Exception : " + ex.toString());
}
System.out.println(array);
return(array);
}
}pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mchugo.que</groupId>
<artifactId>McHugoQue</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>McHugoQue</name>
<description>Que for sending data to email for the compermised passwords</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>我试过的
HashMap<String, String> data = new HashMap<>();
for(int i = 0; i <= 10; i++){
HashMap<String, String> data1 = new HashMap<>();
data1.put("IP", i + "");
data1.put("PORT", i + 1 + "");
data.put("value " + i, data1);
}
System.out.println(data);回溯
2022-04-15 13:22:08.415 INFO 43953 --- [ main] c.m.que.McHugoQue.McHugoQueApplication : Starting McHugoQueApplication using Java 16.0.1 on Macbook-Air.lan with PID 43953 (/Volumes/Drive/FiverrWork/untitled folder/McHugoQue/target/classes started by danielcaminero in /Volumes/Drive/FiverrWork/untitled folder/McHugoQue)
2022-04-15 13:22:08.419 INFO 43953 --- [ main] c.m.que.McHugoQue.McHugoQueApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-15 13:22:10.677 INFO 43953 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 1900 (http)
2022-04-15 13:22:10.698 INFO 43953 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-15 13:22:10.698 INFO 43953 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-15 13:22:10.852 INFO 43953 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-15 13:22:10.852 INFO 43953 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2295 ms
2022-04-15 13:22:11.513 INFO 43953 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 1900 (http) with context path ''
2022-04-15 13:22:11.535 INFO 43953 --- [ main] c.m.que.McHugoQue.McHugoQueApplication : Started McHugoQueApplication in 4.137 seconds (JVM running for 5.008)
2022-04-15 13:22:21.712 INFO 43953 --- [nio-1900-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-15 13:22:21.712 INFO 43953 --- [nio-1900-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-15 13:22:21.713 INFO 43953 --- [nio-1900-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
{"Value 1":{"Username":"johnsen@cityeyes.dk","Password":"nodea03"}}
2022-04-15 13:22:22.181 WARN 43953 --- [nio-1900-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]发布于 2022-04-15 17:57:38
您应该使用列表,而不是将值放在对象中并增加计数器变量。我建议你用这样的东西
....
//grabs connection details
ConnectionDetails connectionDetails = new ConnectionDetails();
String username = "";
String password = "";
JSONArray array = new JSONArray();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://" + connectionDetails.getHost() + "/" + connectionDetails.getDatabase(), connectionDetails.getUsername(), connectionDetails.getPassword());
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT * FROM credentials WHERE identifier = '" + Username + "'");
while(res.next()){
JSONObject data = new JSONObject();
String username = res.getString("identifier");
String password = res.getString("password");
data.put("Username", username);
data.put("Password", password);
array.add(data);
}
}
catch(Exception ex){
System.out.println("Exception : " + ex.toString());
}
System.out.println(data);
return(data);您的输出看起来略有不同。
[
{
"Username: "firstUserName",
"Password": "somePass"
},
{
"Username: "secondUserName",
"Password": "someOtherPass"
}
]但是,更好的方法是使用列表/数组。否则,您将不得不通过递增变量来访问您的结果,直到引发错误或类似的情况。
https://stackoverflow.com/questions/71887154
复制相似问题