Getting Json response in Spring MVC using pebble template - spring

I'm using pebble template for my Spring MVC application. Is there a way to return JSON response object back instead of HTML template to the client for a particular API call?
When I try to send the JSON object I'm getting the following exception.
com.mitchellbosecke.pebble.error.LoaderException: Could not find template "templates/test/1.html" (?:?)
at com.mitchellbosecke.pebble.loader.ClasspathLoader.getReader(ClasspathLoader.java:74)
at com.mitchellbosecke.pebble.loader.ClasspathLoader.getReader(ClasspathLoader.java:26)
....
....
Caused by
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

I just needed to add #ResponseBody in my method and that fixed the issue.

Related

Get Jackson ObjectMapper in Quarkus

I am writing a custom OpenApiConfigurator that adds some examples to my api dynamically.
When I add the examples using the value field of io.smallrye.openapi.api.models.examples.ExampleImpl, which is an object, the example is null in swagger-ui. It only works when I added the actual json.
To add the actual json I have to generate it from my response dto using Jackson. But how can I access the quarkus object mapper, for which I have some customisations using ObjectMapperCustomizer, if in the OpenApiConfigurator CDI is not available?
It's actually possible to access the CDI container statically with Arc.container().instance(ObjectMapper::class.java).get()
That solved it for me.

Output is not coming in JSON format when NoHandlerFoundException coming

I am using spring 4.X to develop rest api using annotation configuration. I have added servlet.setThrowExceptionIfNoHandlerFound(true) to send NoHandlerFoundException. Created one GlobalExceptionHandler to handle all exception. My question is if NoHandlerFoundException occurs than output is not coming in JSON Format. Do i need to add anything in my servlet configuration

Freemarker missing spring message

I am loading asynchronously (Ajax call to Spring MVC Controller) a free marker template. But i get error message complaining about missing message property.
This is my Spring controller method
#RequestMapping('/messages/{messageId}')
public String conversation(#PathVariable('messageId') Long messageId, ModelMap model) {
//BUILD CONVERSATION MODEL model.put....
"async/conversation"
}
async/conversation.ftl
.....<#spring.message "user.conversation.title"/>.....
I have message properties with
user.conversation.title Conversation
This message is working just fine in other scenario, i think there is some issue loading the template asynchronously.
Solved: I had forgotten to make an import.
<#import "/spring.ftl" as spring/>

ServiceStack Receiving posts without entity

I've a scenario where I get changing post content. So I can't map it to an entity.
I need is to get the json body of the post.
I would like to create an entity with a Property "JSON" so if the url for this entity is called the body is filled.
Is there a way to do this ? or any other way fo have a generic endpoint for posts?
In WebAPI I created a parameterless method on a controller and analysed the body on my own.
See this section on Reading in and De-Serializing ad-hoc custom requests in ServiceStack's wiki.
E.g. you can use a custom request binder or get your Request DTO to implement IRequiresRequestStream to tell ServiceStack to skip the deserialization of the request DTO and directly retrieve the stream without populating the request DTO.

How to use annotated Spring controllers with Freemarker to return custom ajax respones

I primarily use annotated controllers that return ModelAndView object used by Spring and Freemarker to render and return pages to browsers - works great. I'd now like to respond to ajax gets and posts that return html fragments as payload inside an XML message.
The problem I'm having not returning a ModelAndView objects seems to confuse Spring WRT to #ModelAttribute (s) and session objects that support data to / and from forms.
I've had to hack the freemarker template to support i18n messages, etc. I'm finding that simply rendering an FTL file while also trying to utilize Spring's object rendering from froms is becoming quite a rabbit hole.
I'm trying to manually (guess what and) do the things that get done behind the scenes and it's not working optimally. To edit an object I'm manually placing the object in the session on rendering the edit form. When the post comes back there are residual/different values in the object I get back out of the session AND the ModelAttribute that Spring rendered for me.
Bottom line? Questions about Spring, Freemarker and custom non-ModelAndView responses that are rendered by Freemarker.
Add another instance of FreeMarkerViewResolver that only that has viewNames ='*.frg'. The viewClass should be your own class that renders the template the way you want by overriding the processTemplate method.

Resources