Java 缺少拼写字符的 REST 响应值

Java REST response value with missing spelling char

我在 Java 中编写了 REST,但 JSON 响应消息无效。

我在单个文件中定义了消息 messages.properties。我希望回复应该是这样的:

NOT_FOUND_PERSON = Person doesn't exist

但是我得到的回复是缺少拼写:

['errorMsg': 'Person doesnt exist.']

问题出在哪里?不会是因为配置中 ResourceBundleMessageSource 设置错误?我注意到缺少 UTF8 编码。 是不是某些隐藏转义函数有问题?

我在 JSON 字符串中使用了双引号。

{
    "objectDetail": {
        "id": "1",
        "anotherId": "1",
        "errorText": "This element doesn't exist."
    }
}

我试过你的数据。

['errorMsg': 'Person doesn't exist.']

这是我的示例,它 returns 一个转换为 JSON 字符串的对象。

@RestController
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public class JsonRestController {

    @RequestMapping("/api/{id}")
    public ResponseEntity<RestObject> getModel(@PathVariable Long id) {
         RestObject restObject = restService.getModel(id);
         return new ResponseEntity<>(restObject, HttpStatus.OK);
    }
}

它转义了双引号。

{
    "timestamp": "2018-01-23 13:37:53",
    "title": "Rest object. This is the error does\"t and don't\" aaa.",
    "fullText": "This is the full text. ID: 3",
    "id": 3,
    "value": 0.449947838273684
}

https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

我找到了解决方案。问题出在 messages.properties 中的引号。 默认情况下,spring 使用消息包来表示来自属性文件的消息。任何引号必须用单引号转义,否则将无法正常显示。

这个

test.message2={0}'s message

必须换成这个

test.message2={0}''s message

资源: https://www.mscharhag.com/java/resource-bundle-single-quote-escaping