O
O
O. J2016-03-01 07:49:35
PostgreSQL
O. J, 2016-03-01 07:49:35

Remove quote escaping in json response of Spring+Hibernate+Postgresql?

I am writing a microservice using spring-boot+hibernate+psql
View data structure.
id, UserID type int, UserProfile type json.
The first two are clear.
The UserProfile contains an object of type json -

"{ "name": "Book the First", "author": { "first_name": "Bob", "last_name": "White"} }"

When fetching from a table, I put the json object into a String
@Column(name="UserProfile")
    private String userProfile;




 public String getUserProfile() {
        return userProfile;
    }

And I send json as a response, getting the output
{ \"name\": \"Book the First\", \"author\": { \"first_name\": \"Bob\", \"last_name\": \"White\" } }

How to give json response without backslash without sticking replace?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idyoshin, 2016-03-10
@OrlovEvgeny

you need to work out the transport object, which will be mapped to / from the JSON representation.
Or it’s cheap and cheerful to work with Map, then you can do it like this:

@Inject
    private JsonParser jsonParser;

    @RequestMapping(value="/user_profile/{id}", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> get(@PathVariable("id") Long id) {
        UserEntity entity = userRepository.get(id);
        Map<String, Object> profile = jsonParser.parseMap(entity.getUserProfile());
        return new ResponseEntity(profile, HttpStatus.OK);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question