JAX-RS Jersey REST api 调用映射
JAX-RS Jersey REST api call mapping
我正在尝试在 jersey/java 中使用 Rest 服务。
我请求的 link 类似于
https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT,FB,AAPL&apikey=demo 注意这是一个演示 link。
JSON响应如下:
{
"Meta Data": {
"1. Information": "Batch Stock Market Quotes",
"2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
"3. Time Zone": "US/Eastern"
},
"Stock Quotes": [
{
"1. symbol": "MSFT",
"2. price": "87.8300",
"3. volume": "18638820",
"4. timestamp": "2018-01-10 16:00:00"
},
{
"1. symbol": "FB",
"2. price": "187.8100",
"3. volume": "10515752",
"4. timestamp": "2018-01-10 16:00:00"
},
{
"1. symbol": "AAPL",
"2. price": "174.2500",
"3. volume": "23771860",
"4. timestamp": "2018-01-10 16:00:00"
}
]
}
我的问题是如何在 java class 中正确映射它?
这是我试过的
StockREST.java
public class StockREST {
private String symbol;
private float price;
private float volume;
private String timestamp;
public String getStockQuotes() {
return StockQuotes;
}
public void setStockQuotes(String StockQuotes) {
this.StockQuotes = StockQuotes;
}
private String StockQuotes;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getVolume() {
return volume;
}
public void setVolume(float volume) {
this.volume = volume;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
return "Symbol = " + symbol + " price, = "
+ price + ", volume = " + volume + ", timestamp = " + timestamp;
}
}
和
RESTclient.java
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
/**
*
* @author Me
*/
public class RESTclient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
StockREST exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(StockREST.class);
String symbol = exchange.getSymbol();
float price = exchange.getPrice();
float volume = exchange.getVolume();
String timestamp = exchange.getTimestamp();
System.out.println(exchange);
client.close();
}
}
SOUT 消息打印
Symbol = null price, = 0.0, volume = 0.0, timestamp = null
我试过 arrays/list 但仍然很困惑。谢谢
编辑
已更新RESTClient.java
public class RESTclient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Response exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(Response.class);
// String symbol = exchange.getSymbol();
MetaData metaData = exchange.getMetaData();
List<StockQuote> stockQuotes = exchange.getStockQuotes();
//float price = exchange.getPrice();
//float volume = exchange.getVolume();
//String timestamp = exchange.getTimestamp();
System.out.println(exchange);
client.close();
}
}
首先,您返回的对象不是 StockREST,而是一个您可以调用 Response 的组合对象,需要将其映射到 class Response.class
.get(StockREST.class);
成为
.get(Response.class);
那么无效的java字段名必须映射到有效的java字段名
"1.symbol" 必须映射到对 java 有效的内容,例如 "symbol"
下面是 class 具有适合您的情况的准确映射(省略了 getter 和 setter)
@XmlRootElement
public class Response {
@XmlAttribute(name = "Meta Data")
private MetaData metaData;
@XmlAttribute(name = "Stock Quotes")
private List<StockQuote> stockQuotes;
}
public class MetaData {
@XmlAttribute(name = "1. Information")
private String information;
@XmlAttribute(name = "2. Notes")
private String notes;
@XmlAttribute(name = "3. Time Zone")
private String timeZone;
}
public class StockQuote {
@XmlAttribute(name = "1. symbol")
private String symbol;
@XmlAttribute(name = "2. price")
private float price;
@XmlAttribute(name = "3. volume")
private float volume;
@XmlAttribute(name = "4. timestamp")
private String timestamp;
}
编辑:
您还需要一个安全的 url (https),因此您需要提供一个包含可信 ssl 证书的信任库来访问您的 url
以下代码绕过错误的证书检查
public static void main(String[] args) {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println(e);
}
Client client = ClientBuilder.newBuilder().sslContext(sc).build();
Response exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(Response.class);
System.out.println(exchange.getMetaData());
for (StockQuote sq : exchange.getStockQuotes()) {
System.out.println(sq);
}
client.close();
}
作业完成,
享受
我正在尝试在 jersey/java 中使用 Rest 服务。 我请求的 link 类似于
https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT,FB,AAPL&apikey=demo 注意这是一个演示 link。
JSON响应如下:
{
"Meta Data": {
"1. Information": "Batch Stock Market Quotes",
"2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
"3. Time Zone": "US/Eastern"
},
"Stock Quotes": [
{
"1. symbol": "MSFT",
"2. price": "87.8300",
"3. volume": "18638820",
"4. timestamp": "2018-01-10 16:00:00"
},
{
"1. symbol": "FB",
"2. price": "187.8100",
"3. volume": "10515752",
"4. timestamp": "2018-01-10 16:00:00"
},
{
"1. symbol": "AAPL",
"2. price": "174.2500",
"3. volume": "23771860",
"4. timestamp": "2018-01-10 16:00:00"
}
]
}
我的问题是如何在 java class 中正确映射它? 这是我试过的
StockREST.java
public class StockREST {
private String symbol;
private float price;
private float volume;
private String timestamp;
public String getStockQuotes() {
return StockQuotes;
}
public void setStockQuotes(String StockQuotes) {
this.StockQuotes = StockQuotes;
}
private String StockQuotes;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getVolume() {
return volume;
}
public void setVolume(float volume) {
this.volume = volume;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
return "Symbol = " + symbol + " price, = "
+ price + ", volume = " + volume + ", timestamp = " + timestamp;
}
}
和
RESTclient.java
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
/**
*
* @author Me
*/
public class RESTclient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
StockREST exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(StockREST.class);
String symbol = exchange.getSymbol();
float price = exchange.getPrice();
float volume = exchange.getVolume();
String timestamp = exchange.getTimestamp();
System.out.println(exchange);
client.close();
}
}
SOUT 消息打印 Symbol = null price, = 0.0, volume = 0.0, timestamp = null
我试过 arrays/list 但仍然很困惑。谢谢
编辑
已更新RESTClient.java
public class RESTclient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Response exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(Response.class);
// String symbol = exchange.getSymbol();
MetaData metaData = exchange.getMetaData();
List<StockQuote> stockQuotes = exchange.getStockQuotes();
//float price = exchange.getPrice();
//float volume = exchange.getVolume();
//String timestamp = exchange.getTimestamp();
System.out.println(exchange);
client.close();
}
}
首先,您返回的对象不是 StockREST,而是一个您可以调用 Response 的组合对象,需要将其映射到 class Response.class
.get(StockREST.class);
成为
.get(Response.class);
那么无效的java字段名必须映射到有效的java字段名
"1.symbol" 必须映射到对 java 有效的内容,例如 "symbol"
下面是 class 具有适合您的情况的准确映射(省略了 getter 和 setter)
@XmlRootElement
public class Response {
@XmlAttribute(name = "Meta Data")
private MetaData metaData;
@XmlAttribute(name = "Stock Quotes")
private List<StockQuote> stockQuotes;
}
public class MetaData {
@XmlAttribute(name = "1. Information")
private String information;
@XmlAttribute(name = "2. Notes")
private String notes;
@XmlAttribute(name = "3. Time Zone")
private String timeZone;
}
public class StockQuote {
@XmlAttribute(name = "1. symbol")
private String symbol;
@XmlAttribute(name = "2. price")
private float price;
@XmlAttribute(name = "3. volume")
private float volume;
@XmlAttribute(name = "4. timestamp")
private String timestamp;
}
编辑:
您还需要一个安全的 url (https),因此您需要提供一个包含可信 ssl 证书的信任库来访问您的 url
以下代码绕过错误的证书检查
public static void main(String[] args) {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println(e);
}
Client client = ClientBuilder.newBuilder().sslContext(sc).build();
Response exchange = client.target("https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT&apikey=B1KLWMIAGSG0UWYD")
.request(MediaType.APPLICATION_JSON)
.get(Response.class);
System.out.println(exchange.getMetaData());
for (StockQuote sq : exchange.getStockQuotes()) {
System.out.println(sq);
}
client.close();
}
作业完成, 享受