我有这个 Controller 功能
@ResponseBody
@RequestMapping(value = { "/send_now" }, method = RequestMethod.GET)
public void send_now()throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
//HttpPost httpPost = new HttpPost("http://localhost/batch_insert/sender.php");
HttpPost httpPost = new HttpPost("https://hookb.in/vaPG2gkm");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sender", "TRADER"));
params.add(new BasicNameValuePair("phone", "4463839399393"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = client.execute(httpPost);
client.close();
}
我正在尝试发布并在此处查看结果 https://hookbin.com/bin/vaPG2gkm
最终,我希望能够在 php 脚本中捕获发布的数据
error_reporting(E_ALL & ~E_NOTICE);
require ('config.php');
require ('db_class.php');
$db = new MySQLDatabase;
$post = file_get_contents('php://input');
$msg = rand(30,100);
$data = file_get_contents('php://input');
$phone = $data['phone'];
$sender = $data['sender'];
$msg = 'This was sent via the Api';
$time = date('Y-m-d H:i:s');
$sql="insert into clickatellout (sender,receiver,msg,msgtype,status,flag,scheduledtime)
values ('".$sender."','".$phone."','".$msg."','SMS:TEXT','Send','1','".$time."')";
$db->query($sql);
echo 'ok';
当我运行方法send_now
时,没有发布任何数据,并且我在控制台中收到此警告
WARNING: Failed to evaluate Jackson serialization for type [void]: java.lang.IllegalStateException: Failed to instantiate standard serializer (of type com.fasterxml.jackson.databind.ser.std.NullSerializer): Class com.fasterxml.jackson.databind.ser.BasicSerializerFactory can not access a member of class com.fasterxml.jackson.databind.ser.std.NullSerializer with modifiers "private"
为什么该函数没有发布数据?
最佳答案
看来您的 send_now
方法正在返回 Jackson 试图序列化的 void。要解决此问题,您可以尝试返回某些内容或将以下注释添加到您的方法中:
@ResponseStatus(value = HttpStatus.OK)
关于java - 在 Spring Controller 方法中发布到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49624835/