Spring messaging websockets - how to call setMessageCodec - spring

I am trying to use Spring websockets with Genson instead of Jackson. When I try and connect from a client I get the following:
java.lang.IllegalStateException: A SockJsMessageCodec is required but not available: Add Jackson 2 to the classpath, or configure a custom SockJsMessageCodec.
It would appear I need to set a custom message codec.
I can see that a .setMessageCodec method appears on the TransportHandlingSockJsService but I can't see anywhere in the configuration options where I can actually set it.
I think the the .setMessageCodec method should be present on the SockJsServiceRegistration class so it can be set from configuration...but it isn't...any ideas?
EDIT: I believe this is a bug so have raised: https://jira.spring.io/browse/SPR-12091

Have a look at this issue https://jira.spring.io/browse/SPR-11184.
It looks like that you could implement it by overriding the configureMessageConverters method in WebSocketMessageBrokerConfigurer.

Related

In Spring Boot how do you register custom converters that are available when parsing application configuration?

In a Spring Boot application how do you register custom converts to be used when processing application configuration?
I have made a custom convert (org.springframework.core.convert.converter.Converter) so it can be used by the ApplicationConversionService/Binder to parse #ConfiguraitonProperties defined in application.properties and application.yaml configuration files but do not know how to register it.
I have tried the solution here https://stackoverflow.com/a/41205653/45708 but it creates an instance of my converter after the application configuration parameters have been processed.
I ran into this issue myself recently. From what I can tell, the key issue is that binding to configuration properties occurs very early in the Spring startup process, before the Application Context is fully initialized. Therefore the usual methods for registering a converter are not reliable. In fact the ConversionService used for configuration binding appear to be a one-off and not really connected to the ConversionService that is stored in the Application Context.
I was able to get something working but it feels like a hack, as it relies on internal implementation details that may work today but not tomorrow. In any case, this is the code I used:
((ApplicationConversionService) ApplicationConversionService.getSharedInstance()).addConverter(myCustomConverter);
The trick I found was to make sure this gets called as soon as possible at application startup so that it gets called before the configuration binding where it's needed. I put it in a #PostConstruct block inside my main #SpringBootApplication class as this seemed to get invoked early on, at least in my case.

What is exactly server.error.path property?

In Spring Boot, what is the purpose of server.error.path property in application.properties file?
The documentation just says:
Path of the error controller
But I want a clear description of this property with an example.
server.error.path - used as part of url for error pages.
site.getBaseUrl() + "/error"
For example some error happen on server side and you decide redirect user to error page like this:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png
Code example of error controller you can find here:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
You can use this property in #RequestMapping("/error"). But instead of "/error" you can use "${server.error.path}"
UPDATE:
Also, Spring Boot BasicErrorController use server.error.path property
Property server.error.path in spring boot application used to define an error path while dealing with custom error handler. In Spring we create custom error handler using functional interface ErrorController, ths interface has a String type method getErrorPath which helps us to return the error page path(our error page as view).
But from Spring 2.3.0 this getErrorPath() method has been deprecated and replaced with server.error.path to manage the error path.
e.g. server.error.path=/error
For more detail about interface ErrorController, please refer Spring doc for ErrorController

How to log MDC with Spring Sleuth 2.0?

referring to quesition/answer in How to log MDC with Spring Sleuth?
I think this has/will change(d) with spring-cloud 2.0 as there is no SpanLogger or Slf4jSpanLogger anymore (or I don't find it)
Wouldn't it be nice if application properties spring.sleuth.baggage-keys and spring.sleuth.propagation-keys if set would also be put in MDC I think inside Slf4jCurrentTraceContext (as this class is currently final I cannot subclass it)
If not, how could I achieve this with spring-cloud 2.0 accordingly?
We don't want to put all entries in MDC (that really doesn't make a lot of sense). You can however either copy the Slf4jCurrentTraceContext and extend it in the way you want to (and register it as a bean) or maybe create your own implementation of CurrentTraceContext that would wrap the existing CurrentTraceContext via a Bean Post Processor and perform additional logic. I guess the first option is more preferable.
In version 2.1.0, Slf4jScopeDecorator was introduced and it will automatically add baggage values to MDC as long as they are whitelisted in the spring.sleuth.log.slf4j.whitelisted-mdc-keys configuration.
For example, if you have the following configuration:
spring.sleuth.baggage-keys=key1,key2
spring.sleuth.log.slf4j.whitelisted-mdc-keys=key2
Only the value of key2 will be automatically added MDC, but not the value of key1.
For more info, see: https://cloud.spring.io/spring-cloud-sleuth/reference/html/#prefixed-fields

org.springframework.web.servlet.DispatcherServlet.noHandlerFound

If i want to map my source address only i.e. #RequestMapping(value="/"), then apache tomcat gives following error:
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/application-name/]
Any other mapping works totally fine.
1)Make sure that you annotated your class containing handler method (the one annotated with #RequestMapping) with #Controller
2) If you don't use spring boot, you may need this annotation: #ComponentScan(/path to package with components/) in your configuration class.
This will tell where to look for spring components (controllers are one type of them)
It would be great if you show us your configuration and controller files.

SmartLifecycleRoleController get status

How to get the status of the endpoints annotated with #Role that we can start / stop with the SmartLifecycleRoleController?
There doesn't seem to be a method for that, should we use another controller?
It's not currently supported; you'd have to use reflection to get a reference to the lifecycles map and iterate over it.
Or you could subclass it and declare it as a bean with name IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER and intercept all the add calls.
I opened a JIRA Issue to provide access.

Resources