Apache CXF Spring 休息服务 POST 请求 returns "msg" : "Stream closed"
Apache CXF Spring rest service POST request returns "msg" : "Stream closed"
我使用 Apache Cxf,Spring Jax-rs 服务,我有以下服务定义和提供的实现,
定义
@POST
@Path("/generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(final String walletName, String currencyName);
实施
public synchronized WalletInfo generateAddress(final String walletName, String currencyName) {
WalletInfo walletInfo = IWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
return walletInfo;
}
当我用 cURL
发出 POST
请求时
curl -H "Content-Type: application/json" -X POST -d '{"walletName":"Icecream5500","currencyName":"Bitcoin"}' http://localhost:8080/api/rest/wallet/generateAddress
我收到 JSON
回复,
{
"msg" : "Stream closed",
"date" : "2017-08-28T09:22:027Z"
}
我很确定 generateAddress
方法工作正常。什么是
这里的问题,特别是当您在执行 POST
请求时在 Spring Apache Cxf 项目中收到消息 Stream closed
时?显然,如果需要,我可以提供更多信息。服务器日志正常,没有发现异常。
POST
主体与方法的参数不匹配,这首先造成了问题。我已经在以下选项中解决了这个问题。
我新建了一个class
public class CreateWalletWithNameAndCurrency {
String walletName;
String currencyName;
public CreateWalletWithNameAndCurrency(String walletName, String currencyName) {
this.walletName = walletName;
this.currencyName = currencyName;
}
public CreateWalletWithNameAndCurrency() {
}
public String getWalletName() {
return walletName;
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public void setWalletName(String walletName) {
this.walletName = walletName;
}
}
我像这样更改了 POST
请求的定义,
@POST
@Path("generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(CreateWalletWithNameAndCurrency createWalletWithNameAndCurrency);
下面提供了实现,
public synchronized WalletInfo generateAddress(CreateWalletWithNameAndCurrency createWalletWithNameAndCurrency) {
String walletName = createWalletWithNameAndCurrency.getWalletName();
String currencyName = createWalletWithNameAndCurrency.getCurrencyName();
WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
// some more code
}
终于,我可以像这样完成 POST
请求了,
curl -H "Content-Type: application/json" -X POST -d '{"walletName":"Copenhangen","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
我使用 Apache Cxf,Spring Jax-rs 服务,我有以下服务定义和提供的实现,
定义
@POST
@Path("/generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(final String walletName, String currencyName);
实施
public synchronized WalletInfo generateAddress(final String walletName, String currencyName) {
WalletInfo walletInfo = IWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
return walletInfo;
}
当我用 cURL
发出 POST
请求时
curl -H "Content-Type: application/json" -X POST -d '{"walletName":"Icecream5500","currencyName":"Bitcoin"}' http://localhost:8080/api/rest/wallet/generateAddress
我收到 JSON
回复,
{
"msg" : "Stream closed",
"date" : "2017-08-28T09:22:027Z"
}
我很确定 generateAddress
方法工作正常。什么是
这里的问题,特别是当您在执行 POST
请求时在 Spring Apache Cxf 项目中收到消息 Stream closed
时?显然,如果需要,我可以提供更多信息。服务器日志正常,没有发现异常。
POST
主体与方法的参数不匹配,这首先造成了问题。我已经在以下选项中解决了这个问题。
我新建了一个class
public class CreateWalletWithNameAndCurrency {
String walletName;
String currencyName;
public CreateWalletWithNameAndCurrency(String walletName, String currencyName) {
this.walletName = walletName;
this.currencyName = currencyName;
}
public CreateWalletWithNameAndCurrency() {
}
public String getWalletName() {
return walletName;
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public void setWalletName(String walletName) {
this.walletName = walletName;
}
}
我像这样更改了 POST
请求的定义,
@POST
@Path("generateAddress")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
WalletInfo generateAddress(CreateWalletWithNameAndCurrency createWalletWithNameAndCurrency);
下面提供了实现,
public synchronized WalletInfo generateAddress(CreateWalletWithNameAndCurrency createWalletWithNameAndCurrency) {
String walletName = createWalletWithNameAndCurrency.getWalletName();
String currencyName = createWalletWithNameAndCurrency.getCurrencyName();
WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
// some more code
}
终于,我可以像这样完成 POST
请求了,
curl -H "Content-Type: application/json" -X POST -d '{"walletName":"Copenhangen","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress