gpt4 book ai didi

java - Java中删除响应实体Json中的反斜杠

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:01 25 4
gpt4 key购买 nike

我正在测试我的 Rest Controller ,当我在 java 中打印结果时,它们似乎没问题,但在 swagger 或 postman 上它返回斜杠。我该如何删除这些。

在Java中,我用日志打印对象,结果如下:

返回对象

    {
create=Create{
create={
"Apartment": "Flat",
"Drink": {
"Alchol": {
"Beer": {
"Dutch": "Groeshe",
"Zimbo": "Zambezi"
},
"Wine": "pocupine"
},
"NonAlcoholic": {
"fizzy": "Coke",
"juice": "liquifruit"
}
},
"fruit": "Apple",
"size": "Large",
"color": "Red",
"Proffesion": [
"Java",
"Lawyer"
],
"cars": {
"type": "BMW",
"Color": "Brown"
}
}
},
update=null,
delete=null
}

我用谷歌搜索了一下,java中似乎没有任何东西可以解决这个问题。请帮忙

    @RestController
public class ProcessController<T extends Serializable> {


@Autowired
private EngineImpl engine;


@RequestMapping(value = "/api/processEngine", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<ReturnObject<T>> processEngine(@RequestBody final Request request) throws Exception {

ReturnObject<T> response = engine.calculate((T) request.getA(), (T) request.getB());

LOG.info("ReturnObject engine response parameters: " + response);
//Prints: ReturnObject{create=Create{create={"Apartment":"Flat","Drink":{"Alchol":{"Beer":{"Dutch":"Groeshe","Zimbo":"Zambezi"},"Wine":"pocupine"},"NonAlcoholic":{"fizzy":"Coke","juice":"liquifruit"}},"fruit":"Apple","size":"Large","color":"Red","Proffesion":["Java","Lawyer"],"cars":{"type":"BMW","Color":"Brown"}}}, update=null, delete=null}
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

预期:

    {
"create": {
"create": {
"Apartment": "Flat",
"Drink": {
"Alchol": {
"Beer": {
"Dutch": "Groeshe",
"Zimbo": "Zambezi"

},
"Wine": "pocupine"

},
"NonAlcoholic": {
"fizzy": "Coke",
"juice": "liquifruit"
}
},
"fruit": "Apple",
"size": "Large",
"color": "Red",
"Proffesion": ["Java", "Lawyer"],
"cars": {
"type": "BMW",
"Color": "Brown"
}
},
"update": null,
"delete": null
}
}

实际:

    {
"create": {
"create": "{\"Apartment\":\"Flat\",\"Drink\":{\"Alchol\":{\"Beer\":{\"Dutch\":\"Groeshe\",\"Zimbo\":\"Zambezi\"},\"Wine\":\"pocupine\"},\"NonAlcoholic\":{\"fizzy\":\"Coke\",\"juice\":\"liquifruit\"}},\"fruit\":\"Apple\",\"size\":\"Large\",\"color\":\"Red\",\"Proffesion\":[\"Java\",\"Lawyer\"],\"cars\":{\"type\":\"BMW\",\"Color\":\"Brown\"}}"
},
"update": null,
"delete": null
}

最佳答案

你需要使用解析器,以获得漂亮的 JSON 字符串,猜你正在使用 Jackson,所以你可以这样做:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourObject);

关于java - Java中删除响应实体Json中的反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56388326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com