1

I have REST written in Java and the JSON response message is not valid.

I have messages defined in single file messages.properties. I expect that response should be something like that:

NOT_FOUND_PERSON = Person doesn't exist

However I got response with missing spelling:

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

Where is the problem? Cannot be due to wrong setup ResourceBundleMessageSource in config? I noticed there is missing UTF8 coding. Is there problem with some hide escape function or whatever?

Luke
  • 1,163
  • 2
  • 11
  • 19
  • Please, show how your server side is coded. I am interested in Rest Controller or RestResource, whatever framework you use for the rest server. – Yan Khonski Jan 23 '18 at 12:17

2 Answers2

1

I found the solution. The problem was with quotation mark in messages.properties. In default, spring uses message bundle to represent messages from properties file. Any occurences of quotation mark must be escaped by single quote otherwise it won't be displayed properly.

This

test.message2={0}'s message

must be replaced by this

test.message2={0}''s message

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

Luke
  • 1,163
  • 2
  • 11
  • 19
0

I used double quotes in the JSON string.

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

And I tried with your data.

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

This is my example, it returns an object which is converted to JSON String.

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

It escapes double quotes.

{
    "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://stackoverflow.com/a/15637481/4587961

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

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • I have messages defined in single file messages.properties. Cannot be due to wrong setup ResourceBundleMessageSource in config? I noticed there is missing UTF8 coding. I dont know. – Luke Jan 26 '18 at 10:58