Output is not coming in JSON format when NoHandlerFoundException coming - spring

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

Related

Classcastexception for CloudEventMessageConverter to AbstractMessageConverter

when using spring cloud stream latest version with cloud events spring sdk
Facing classcastexception in below scenario.
Message has contentType = application/*+avro.
SmartcompositeMessageConverter contains converter list like below -
CloudEventMessageConverter
AvroSchemaRegisteryClientMessageConverter
and some ootb converters.
While converting to message from producer..
SmartcompositeMessageConverter has this line of code for wildcard contentType
((AbstractMessageConverter) converter).getSupportedMimeTypes()
But CloudEventMessageConverter is not an instance of AbstractMessageConverter, hence it throws an exception.
Please guide what should be overridden in this case.
If I create a new CloudEventMessageConverter which extends AbstractMessageConverter, I cann't add headers as only method allowed to override is convertFromInternal which returns just the payload.

How to access Request headers in Spring Web services PayloadValidatingInterceptor

I am currently working on a Spring Web services(SOAP) project. The requirement is to validate the request payload and return the error response if validation fails and log it. I have extended PayloadValidatingInterceptor to return custom validation message.
As part of logging requirement, we need to print the header values from Request Headers (HttpServletRequest) for tracking purpose.
How to access HttpServletRequest in the implementation of PayloadValidatingInterceptor?
Is it possible to inject HttpServletRequest?
version details:
Spring-Boot : 2.2.6
Spring-ws-core : 3.0.8
Please help.

Getting Json response in Spring MVC using pebble template

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.

Spring Kafka Producer logging

I am trying to develop/debug a Spring KAfka application. I am using KafkaTemplate to serialize a POJO to JSON. However I m unable to log the actual serialized JSON text. Is there any way to log these details? What is the log configuration needed?
It depends on how you are doing the serialization. If you are using a StringJsonMessageConverter in the template, with a StringSerializer, and use send(Message<?>), a ProducerInterceptor configured in the Kafka properties will see the JSON.
If you are using the JsonSerializer it won't work because the interceptor sees the record before serialization. You would have to create your own Serializer that wraps the JsonSerializer and logs the output.

Customize spring boot validation message from database

I am using spring validation to validate the Rest Controller input, I would appreciate if any one can tell me is there a possibility of throwing custom message in case of exception and the custom message should come from database rather than from a properties file.
You can achieve this by setting a custom validation message source on Spring's LocalValidatorFactoryBean.
Your message source needs to implement the MessageSource interface and read its messages from the database.
Reference:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.html#setValidationMessageSource-org.springframework.context.MessageSource-
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSource.html

Resources