1

I am using Spring boot with mvc. I have a json schema for request and response. Basically the user posts data (in json) to a url, the controller does its logic and returns a json. I am having difficulty in returning the json. So far, I have a couple of views (in thymeleaf) with hardcoded responses which I dont want. I just want to use an object which I can edit and send back to the client. The response is very simple. This is the schema of the response:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"message":{
"minLength":1,
"type":"string"

}
},
"required": [
"message"
]
}

I have an object of type response which conforms to my response json schema. I basically want to output this object to the client. But not sure how as my controller returns a String and this string is usually the name of the html page.

1 Answer 1

5

Add the @ResponseBody annotation to your controller method and it will use the returned value as a response rather than as a path to a page.

If you want that all methods of your controller return direct data (and not page paths), you can annotate your controller with @RestController rather than @Controller.

Sign up to request clarification or add additional context in comments.

1 Comment

I've added the @RestController and it worked. Learnt something new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.