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

时间:2018-01-23 11:54:10

标签: java json rest spelling

我使用Java编写REST,JSON响应消息无效。

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

NOT_FOUND_PERSON = Person doesn't exist

但是我得到了拼写错误的答案:

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

问题出在哪里?不能因为配置错误设置ResourceBundleMessageSource?我注意到缺少UTF8编码。 有一些隐藏逃脱功能有什么问题吗?

2 个答案:

答案 0 :(得分:1)

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

test.message2={0}'s message

必须由此

替换
test.message2={0}''s message

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

答案 1 :(得分:0)

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

[G1_size_diff_max , idx] =max(G1_size_diff);
result_Si = G1_Si_size(idx);
result_Aq = G1_Aq_size(idx);

我尝试了你的数据。

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

这是我的示例,它返回一个转换为JSON String的对象。

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

它逃脱了双引号。

@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);
    }
}

https://stackoverflow.com/a/15637481/4587961

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