在Spring boot 项目中 利用@ResponseBody返回JSON数据

m0_37978976 2018-01-24 11:48:26
在项目中碰到了一个问题

现在有一个实体类。
public class TestEntity {
private String html;
private String other;
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
public String getOther() {
return other;
}
public void setOther(String otherl) {
this.other = other;
}
@Override
public String toString() {
return "TestEntity [html=" + html + ", other =" + other + "]";
}
}

@RestController
@RequestMapping("/test")
public class TestController {

@GetMapping("/test")
public TestEntity test(){
TestEntity testEntity = new TestEntity();
//实际数据从数据库中回去,数据保存整个HTML
testEntity.setHtml("<div id=\"0001\"></div>");
testEntity.setOther("id=123456789");
return testEntity;
}
}

利用测试工具postman获取到的数据
{"html":"id=\"123456\"","other":"id=123456789"}

前端正真需要的数据形式是{"html":"<div id="0001"></div>","other":"id=123456789"}
...全文
3184 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
花匠小林 2018-01-25
  • 打赏
  • 举报
回复
抱歉看错了~
我测试了你的代码没问题啊
花匠小林 2018-01-25
  • 打赏
  • 举报
回复
修改getter方法就可以
dong_19890208 2018-01-25
  • 打赏
  • 举报
回复
引用 7 楼 m0_37978976 的回复:
[quote=引用 6 楼 dong_19890208 的回复:] 楼主有没有试过在浏览器调试工具里面看数据?是不是你的工具把<div>给解析了
在浏览器和调试工具上结果是一样的[/quote]我用你的代码测试了一下结果就是{"html":"<div id=\"0001\"></div>","other":"id=123456789"} ,你是不是有其他地方加了过滤器了?
m0_37978976 2018-01-25
  • 打赏
  • 举报
回复
引用 6 楼 dong_19890208 的回复:
楼主有没有试过在浏览器调试工具里面看数据?是不是你的工具把<div>给解析了
在浏览器和调试工具上结果是一样的
dong_19890208 2018-01-25
  • 打赏
  • 举报
回复
楼主有没有试过在浏览器调试工具里面看数据?是不是你的工具把<div>给解析了
m0_37978976 2018-01-25
  • 打赏
  • 举报
回复
引用 4 楼 m0_37978976 的回复:
[quote=引用 1 楼 qq_15259781 的回复:] 修改getter方法就可以
这个不是拼装的问题,看清楚我想变大==表达的意思。[/quote] 万恶的输出法。。。 这个不是拼装的问题,看清楚我想表达的意思
m0_37978976 2018-01-25
  • 打赏
  • 举报
回复
引用 1 楼 qq_15259781 的回复:
修改getter方法就可以
这个不是拼装的问题,看清楚我想变大==表达的意思。
xwn_2016 2018-01-25
  • 打赏
  • 举报
回复
同意一楼,在get方法中把需要得到的格式拼好
Defonds 2018-01-25
  • 打赏
  • 举报
回复
" 里的 " 是不是该换成 '

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧