我需要使用第三方RESTful API。这是我的控制器类:
@Controller
public class BrokerController {
@RequestMapping(value = "/broker", method = RequestMethod.POST)
public @ResponseBody MyPojo brokers(@RequestBody BrokerRequest brokerRequest){
RestTemplate restTemplate = new RestTemplate();
final String uri = "http://www.nepalipaisa.com/Modules/MarketMovers/Services/MarketMoversServices.asmx/GetTopBrokers";
MyPojo myPojo = restTemplate.postForObject(uri,brokerRequest,MyPojo.class);
return myPojo;
}
}
编辑: - 我的pojo课程:
public class MyPojo {
private String __type;
private Integer RowTotal;
private Integer StockID;
private Object CodedCompany;
private Object ClosingPrice;
private Integer Amount;
private Integer PreviousClosing;
private Integer DifferenceRS;
private Object Symbol;
private Integer Price;
private Integer Diff;
private Integer PercentageDiff;
private Object Volume;
private Integer TotalTurnOverAmount;
private String FirmName;
private Integer BrokerID;
private String BrokerCode;
private Integer TotalTransactions;
private Object Traded;
private Object MaxPrice;
private Object MinPrice;
private Object OpeningPrice;
private Object TotalShare;
private Integer NoOfTransaction;
private Integer Purchase;
private Integer Sales;
private Integer Matching;
public String get__type() {
return __type;
}
public void set__type(String __type) {
this.__type = __type;
}
public Integer getRowTotal() {
return RowTotal;
}
public void setRowTotal(Integer rowTotal) {
this.RowTotal = rowTotal;
}
public Integer getStockID() {
return StockID;
}
public void setStockID(Integer stockID) {
this.StockID = stockID;
}
public Object getCodedCompany() {
return CodedCompany;
}
public void setCodedCompany(Object codedCompany) {
this.CodedCompany = codedCompany;
}
public Object getClosingPrice() {
return ClosingPrice;
}
public void setClosingPrice(Object closingPrice) {
this.ClosingPrice = closingPrice;
}
public Integer getAmount() {
return Amount;
}
public void setAmount(Integer amount) {
this.Amount = amount;
}
public Integer getPreviousClosing() {
return PreviousClosing;
}
public void setPreviousClosing(Integer previousClosing) {
this.PreviousClosing = previousClosing;
}
public Integer getDifferenceRS() {
return DifferenceRS;
}
public void setDifferenceRS(Integer differenceRS) {
this.DifferenceRS = differenceRS;
}
public Object getSymbol() {
return Symbol;
}
public void setSymbol(Object symbol) {
this.Symbol = symbol;
}
public Integer getPrice() {
return Price;
}
public void setPrice(Integer price) {
this.Price = price;
}
public Integer getDiff() {
return Diff;
}
public void setDiff(Integer diff) {
this.Diff = diff;
}
public Integer getPercentageDiff() {
return PercentageDiff;
}
public void setPercentageDiff(Integer percentageDiff) {
this.PercentageDiff = percentageDiff;
}
public Object getVolume() {
return Volume;
}
public void setVolume(Object volume) {
this.Volume = volume;
}
public Integer getTotalTurnOverAmount() {
return TotalTurnOverAmount;
}
public void setTotalTurnOverAmount(Integer totalTurnOverAmount) {
this.TotalTurnOverAmount = totalTurnOverAmount;
}
public String getFirmName() {
return FirmName;
}
public void setFirmName(String firmName) {
this.FirmName = firmName;
}
public Integer getBrokerID() {
return BrokerID;
}
public void setBrokerID(Integer brokerID) {
this.BrokerID = brokerID;
}
public String getBrokerCode() {
return BrokerCode;
}
public void setBrokerCode(String brokerCode) {
this.BrokerCode = brokerCode;
}
public Integer getTotalTransactions() {
return TotalTransactions;
}
public void setTotalTransactions(Integer totalTransactions) {
this.TotalTransactions = totalTransactions;
}
public Object getTraded() {
return Traded;
}
public void setTraded(Object traded) {
this.Traded = traded;
}
public Object getMaxPrice() {
return MaxPrice;
}
public void setMaxPrice(Object maxPrice) {
this.MaxPrice = maxPrice;
}
public Object getMinPrice() {
return MinPrice;
}
public void setMinPrice(Object minPrice) {
this.MinPrice = minPrice;
}
public Object getOpeningPrice() {
return OpeningPrice;
}
public void setOpeningPrice(Object openingPrice) {
this.OpeningPrice = openingPrice;
}
public Object getTotalShare() {
return TotalShare;
}
public void setTotalShare(Object totalShare) {
this.TotalShare = totalShare;
}
public Integer getNoOfTransaction() {
return NoOfTransaction;
}
public void setNoOfTransaction(Integer noOfTransaction) {
this.NoOfTransaction = noOfTransaction;
}
public Integer getPurchase() {
return Purchase;
}
public void setPurchase(Integer purchase) {
this.Purchase = purchase;
}
public Integer getSales() {
return Sales;
}
public void setSales(Integer sales) {
this.Sales = sales;
}
public Integer getMatching() {
return Matching;
}
public void setMatching(Integer matching) {
this.Matching = matching;
}
}
JSON iI需要消耗,即下面给出的确切结果:
{
"d": [
{
"__type": "SageFrame.MarketMovers.MarketInfo",
"RowTotal": 0,
"StockID": 0,
"CodedCompany": null,
"ClosingPrice": null,
"Amount": 0,
"PreviousClosing": 0,
"DifferenceRS": 0,
"Symbol": null,
"Price": 0,
"Diff": 0,
"PercentageDiff": 0,
"Volume": null,
"TotalTurnOverAmount": 109969058,
"FirmName": "Vision Securities Pvt. Ltd.",
"BrokerID": 0,
"BrokerCode": "34",
"TotalTransactions": 0,
"Traded": null,
"MaxPrice": null,
"MinPrice": null,
"OpeningPrice": null,
"TotalShare": null,
"NoOfTransaction": 0,
"Purchase": 70691939,
"Sales": 39277119,
"Matching": 6381555
},
{
"__type": "SageFrame.MarketMovers.MarketInfo",
"RowTotal": 0,
"StockID": 0,
"CodedCompany": null,
"ClosingPrice": null,
"Amount": 0,
"PreviousClosing": 0,
"DifferenceRS": 0,
"Symbol": null,
"Price": 0,
"Diff": 0,
"PercentageDiff": 0,
"Volume": null,
"TotalTurnOverAmount": 104830489,
"FirmName": "Online Securities Pvt. Ltd.",
"BrokerID": 0,
"BrokerCode": "49",
"TotalTransactions": 0,
"Traded": null,
"MaxPrice": null,
"MinPrice": null,
"OpeningPrice": null,
"TotalShare": null,
"NoOfTransaction": 0,
"Purchase": 51927902,
"Sales": 52902587,
"Matching": 3049044
}
]
}
目前我发送带有两个参数的POST请求
{
"offset":"1",
"limit":"2000"
}
我可以得到上面给出的结果。现在我需要通过我自己的控制器返回相同的结果。上面的控制器返回MyPojo
类,每个属性都带有null
值。
答案 0 :(得分:1)
您始终可以尝试将第三方API的结果放入地图中。
Map results = restTemplate.postForObject(uri,brokerRequest, Map.class);
或者你可以为第三部分API创建一个新的pojo。
ThirdPartyPojo results = restTemplate.postForObject(uri,brokerRequest, ThirdPartyPojo.class);
答案 1 :(得分:1)
这很容易实现,甚至不需要你提供自定义映射或类似的东西,并保证你的控制器会提供相同的响应(不过相同的标题):
@RestController
final class Controller {
// Both RestTemplate and URI instances can be cached
private static final RestTemplate restTemplate = new RestTemplate();
private static final URI testUri = URI.create("http://www.nepalipaisa.com/Modules/MarketMovers/Services/MarketMoversServices.asmx/GetTopBrokers");
// I'm using GET just to simplify the testing using a web browser
@RequestMapping(method = GET, value = "/")
public void post(final ServletResponse response)
throws IOException {
// Create a POST request entity
final RequestEntity<?> requestEntity = new RequestEntity<>(getRequest(0, 10), POST, testUri);
// And send the request to the remote server
final ResponseEntity<Resource> responseEntity = restTemplate.exchange(requestEntity, Resource.class);
// Now just copy the response input stream to the output stream of this controller
try ( final InputStream inputStream = responseEntity.getBody().getInputStream() ) {
// Or provide HttpServletResponse via the controller method to be able to configure the response more accurately
StreamUtils.copy(inputStream, response.getOutputStream());
}
}
private static Object getRequest(final long offset, final long limit) {
final Map<String, Object> request = new HashMap<>();
request.put("offset", offset);
request.put("limit", limit);
return request;
}
}
远程服务器可以使用不成功的响应代码进行响应,因此您还可以获得自定义控制器建议来处理RestTemplate
例外:
@ControllerAdvice
final class ExceptionControllerAdvice {
@ExceptionHandler(HttpServerErrorException.class)
@ResponseBody
public ResponseEntity<?> handleHttpServerErrorException(final HttpServerErrorException ex) {
return new ResponseEntity<Object>("Bad gateway: " + ex.getMessage(), BAD_GATEWAY);
}
}
答案 2 :(得分:0)
目前问题是您的pojo MyPojo
与第三方API的响应不兼容。
你的pojo应该是跟随的。
----------------------------------- com.example.D.java ----- ------------------------------
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
public class D {
private String type;
private Integer rowTotal;
private Integer stockID;
private Object codedCompany;
private Object closingPrice;
private Integer amount;
private Integer previousClosing;
private Integer differenceRS;
private Object symbol;
private Integer price;
private Integer diff;
private Integer percentageDiff;
private Object volume;
private Integer totalTurnOverAmount;
private String firmName;
private Integer brokerID;
private String brokerCode;
private Integer totalTransactions;
private Object traded;
private Object maxPrice;
private Object minPrice;
private Object openingPrice;
private Object totalShare;
private Integer noOfTransaction;
private Integer purchase;
private Integer sales;
private Integer matching;
// Getter .. Setter//
}
----------------------------------- com.example.MyPojo.java ----- ------------------------------
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
public class Example {
@JsonProperty("d")
private List<D> d = null;
// Getter ... Setter
}
或者只是你可以使用Map。