ResponseEntity在spring boot中返回String而不是xml类型

时间:2017-01-05 10:06:20

标签: spring tomcat spring-boot

我用过spring和weblogic, 我有这样的方法:

@RequestMapping(value = "/all/{parentId}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getAll(@PathVariable int parentId) {
    StringBuffer returnValue = new StringBuffer("");
    Power power = iPowerService.loadByEntityId(parentId);
    makeTree(power, returnValue);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
    return new ResponseEntity<String>(returnValue.toString(), responseHeaders, HttpStatus.CREATED);
}

上面的方法为树生成xml格式并将其作为响应发送给客户端,如下所示:

<201 Created,
<item text="primary" id="740167402" im0="Mehvar_V.png"
    im1="Mehvar_V.png" im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t740167402" />
</item>
<item
    text="fff"
    id="740168675" im0="Mehvar_V.png" im1="Mehvar_V.png" im2="Mehvar_V.png"
    isVirtual="true">
    <item text="..." im0="leaf.gif" id="t740168675" />
</item>
<item text="mmmehdi" id="12" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t12" />
</item>
<item text="province" id="123" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t123" />
</item>
<item text="power" id="1234" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t1234" />
</item>
,{Content-Type=[text/html; charset=UTF-8]}>

并且客户端将此响应转换为树并显示它 它成功地工作了 当我迁移到spring boot时,此方法的响应将转换为字符串,如下所示:

"<item text="primary" id="740167402" im0="Mehvar_V.png"
    im1="Mehvar_V.png" im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t740167402" />
</item>
<item
    text="fff"
    id="740168675" im0="Mehvar_V.png" im1="Mehvar_V.png" im2="Mehvar_V.png"
    isVirtual="true">
    <item text="..." im0="leaf.gif" id="t740168675" />
</item>
<item text="mmmehdi" id="12" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t12" />
</item>
<item text="province" id="123" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t123" />
</item>
<item text="power" id="1234" im0="Mehvar_V.png" im1="Mehvar_V.png"
    im2="Mehvar_V.png" isVirtual="true">
    <item text="..." im0="leaf.gif" id="t1234" />
</item>"

如您所见"" SURROUND my output

发生了什么?为什么会发生? 为什么我的回复会向客户发送字符串 我希望输出与成功运行相同

1 个答案:

答案 0 :(得分:0)

使用@ResponseBody,您不必将响应包装在新的ResponseEntity中,您可以这样做:

@RequestMapping(value = "/all/{parentId}", method = RequestMethod.GET)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String getAll(@PathVariable int parentId) {
  StringBuffer returnValue = new StringBuffer("");
  ...
  return returnValue.toString()
}

或者你可以juste删除响应正文:

@RequestMapping(value = "/all/{parentId}", method = RequestMethod.GET)
public ResponseEntity<String> getAll(@PathVariable int parentId) {