springboot在restcontroller下返回文本类型

最近本人做一个接口,接口需要用post application/x-www-form-urlencoded发送请求,返回类型是text/plain,刚开始的思路毫无思路,于是在网上找到thymeleaf,其作为springboot组成组件,可以在controller模式下返回文本类型,即在html中打开,可是使用过后发现其对接口post请求无法做出正确的相应,后面解决方式是利用了ResponseEntity,此组件可以自定义返回的文本类型,成功解决问题。项目代码如下:

@RestController
public class MifiController {


@Autowired
private MififlowstatisticsService mififlowstatisticsService;


@RequestMapping(value = "/api/mi-fi/config", method = RequestMethod.GET)
public ResponseEntity<String> whitelist() {
return ResponseEntity.ok().
header("Content-Type", "text/plain").
body("whitelist_domain1=itunes.apple.com \r\n" + 
"whitelist_domain2=*.itunes.apple.com \r\n" + 
"whitelist_domain3=*.phobos.apple.com \r\n" + 
"whitelist_domain4=*.aaplimg.com  \r\n" + 
"whitelist_domain5=*.mzstatic.com  \r\n" + 
"stats_clear_interval=180 ");
}


@RequestMapping(value = "/api/mi-fi/stats", method = RequestMethod.POST)
public ResponseEntity<String> reset(@RequestParam(value = "mac", required = true) String mac,
@RequestParam(value = "imei", required = true) String imei,
@RequestParam(value = "start_at", required = true) Integer start_at,
@RequestParam(value = "up", required = true) Integer up,
@RequestParam(value = "down", required = true) Integer down, 
HttpServletRequest request) {
Calendar c = Calendar.getInstance();
// int seconds = 1521529976;//数据库中提取的数据
long millions = new Long(start_at).longValue() * 1000;
c.setTimeInMillis(millions);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(c.getTime());
Date date= null;
try {
date = DateUtil.formatToDate(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 系统时间
Date systemdate = new Date();
long systime = systemdate.getTime();
c.add(Calendar.HOUR_OF_DAY, 3);
Date clientdate = c.getTime();
long clienttime = clientdate.getTime();
if (systime == clienttime) {
return ResponseEntity.ok().header("Content-Type", "text/plain").body("reset=true");
} else {
return ResponseEntity.ok().header("Content-Type", "text/plain").body("reset=false");
}
}
在此记录,希望对你有所帮助,另外要说一点,此接口的提交的方式是curl -d post ,但是在服务端接收请求时只和post还有get请求有关,和使用哪种框架下接收无关(本人使用springboot),post测试工具,有fiddler,postman,insomina,firefox自带扩展工具等,自行测试。

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值