java - 在 Spring Controller 方法中发布到远程服务器

标签 java php spring

我有这个 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/

相关文章:

java - 是否可以在IDEA中使用 -Dfoo.path=${env_var :FOO_ENV_KEY}?

Java:线程中的 NotifyObserver 和 DeadLock

php - 将多个复选框值插入 MySQL

php - 插入数据库时​​字符损坏

spring - 如何在 Spring 启动测试中模拟spring amqp/rabbit

java - 在半径内随机生成一个 latlng 会产生一个超出范围的点

javascript - html 包含许多类似的选择,这些选择在更改时会使用react

java - Spring 安全 : How to use multiple URL patterns in FilterRegistrationBean?

java - logback.xml - 动态检测目录分隔符

java - 将 Lucene 从 java 1.8 迁移到 1.6 路径 FSDirectory