spring-boot rest application on weblogic - spring

I have a spring boot rest application working fine on Tomcat.
My goal was to deploy the same on weblogic. I have followed the steps mentioned on spring site here
I don't have a web.xml file in the project.
After deployment I ran into this error.
java.lang.IllegalStateException: No thread-bound request found:
Are you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If you are actually operating with
in a web request and still receive this message, your code is probably running outside
of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener
or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentR
equestAttributes(RequestContextHolder.java:131) ~[spring-web-4.3.13.RELEASE.jar:
4.3.13.RELEASE]
The same requests work fine in tomcat. I am not sure if I need to include a web.xml file with ContextLoaderListener. But I wonder how it's not a problem in tomcat container.

Related

Amazon SQS Listener on Spring Boot calling REST endpoint fails with "Are you referring to request attributes outside an actual web request"

I have an Amazon SQS Listener configured in a Spring Boot application. After receiving a message of the queue, it has to call a REST API.
The call fails with this error
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
The REST Template is trying to read some attributes off the RequestContextHolder and it doesn't have the Web context. Unfortunately, I don't have access to the Rest template to change it (it is in a library).
What can I do in this case?

Spring Security with SOAP web service is working in Tomcat, but not in WebLogic

I have created a sample SOAP Web Service project (spring boot) and trying to integrate Okta as a resource server for authentication.
I am able to deploy the application to WebLogic, but when testing the service using SOAP UI, it gives the response even when there is no Token included in the header.
When I access WSDL from a browser using my wsdl url, http://myhost:port/appservice/app.wsdl I see the 401 error, so I think it is picking up the Security config changes. But it is not working for SOAP requests, I would get response even with out Okta token.
Is it because for SOAP requests, do I need to include any interceptors on top of Security Config java file. Or am I taking a wrong path for security with SOAP. Can someone let me know what am I missing or point me to right direction. Is token validation part of WS-Security? or the authentication manager in Okta resource server enough for this?
I followed this documentation to create it.
I have read most of the SO questions related to this and spring documentation, but could not connect the missing dots. Please help me with this. After spending lot of time, I felt like I was moving in circles.
UPDATE:
I have enabled spring security debug logs by doing below
#EnableWebSecurity(debug=true)
logging.level.org.springframework.security.web.FilterChainProxy=DEBUG
UPDATE2:
I haven't made any big changes to my configuration, but when I ran the project on embedded tomcat locally, it started working. To run on Tomcat, I changed packaging from war to jar, excluded Tomcat in my POM and in my Main class, I had to remove the SpringBootServletInitializer and WebApplicationInitializer. That's it. I tested SOAP UI with the Okta bearer token and it gave me response. With out the token it did not give me response.
Spring Security not working only in case of WebLogic12c. I don't know what I am missing to include for that to work in WebLogic. when deployed through Tomcat, request is passed through all the beans in Security Filter Chain {
WebAsyncManagerIntegrationFilter,
SecurityContextPersistenceFilter,
HeaderWriterFilter,
CsrfFilter,
LogoutFilter,
BearerTokenAuthenticationFilter,
RequestCacheAwareFilter,
SecurityContextHolderAwareRequestFilter,
AnonymousAuthenticationFilter,
SessionManagementFilter,
ExceptionTranslationFilter,
FilterSecurityInterceptor}
But on WebLogic, the request is passed only through first four beans in Security Filter Chain {WebAsyncManagerIntegrationFilter,
SecurityContextPersistenceFilter,
HeaderWriterFilter,
CsrfFilter}
I just wanted to update the alternate solution I found for this problem, for completeness.
Spring Security Filter chain was not working for Weblogic, where as same was working in Tomcat, even for Weblogic version 12.2.1.4.
I had followed this example, and implemented Okta filter as spring boot version was not working in Weblogic 12.2.1.4.
Thanks to #Toerktumlare, I have implemented logging with logback-spring.xml

SpringBoot Rest API: Works as Java Application, fails when deployed on tomcat

I have a Springboot project and it has a api which I can invoke from postman.
When I run the application using Main Class I'm able to hit the endpoint and get response.
But if I deploy it on tomcat using war of project the same endpoint says 404!
What am I missing?
There can be multiple possibilities
You didn't extended your application class to SpringBootServletInitializer. In this case, spring boot application will not be deployed to tomcat. To fix this add "extends SpringBootServletInitializer" to your main application class
You are hitting wrong url. Make sure you append your aplication name to url. Example - if http://localhost:8080/data works in your local, and your application name is app, you have to hit http://{{serverip:port}}/app/data when deployed
There is something wrong in application properties like DB configured is local and not accessible from tomcat etc. To check such issues, check your tomcat log file (/{{tomcat dir}}/logs/catalina.out

Groovy Spring Boot REST service is throwing PageNotFound errors for valid paths

I have created a spring-boot-troubleshooting repo on GitHub that reproduces this error exactly.
I am building a Spring Boot-based REST service that will only be exposing RESTful API endpoints, absolutely no UI/views/HTML pages whatsoever.
You can see in that repo's build.gradle that the only two Spring Boot dependencies I depend on are:
,'org.springframework.boot:spring-boot-starter-jetty'
,'org.springframework.boot:spring-boot-starter-actuator'
So nothing view-related (Thymeleaf, etc.).
When you run that app and open a browser to go to the FizzbuzzResource URL you'll get:
Problem accessing /error. Reason:
Not Found
And then on the console you'll see:
WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/v1/fizzbuzz/12345] in DispatcherServlet with name 'dispatcherServlet'
I'm wondering why Spring is complaining HTML-basedPageNotFound errors when this should be a pure REST service, and of course, I'd love to know what the fix is to get this working and returning my Fizzbuzz resource properly!
You need to enable web mvc and make ApiVersionRequestMappingHandlerMapping a bean in the container otherwise Spring won't use your ApiVersionRequestMappingHandlerMapping#getMappingForMethod.
See my commit https://github.com/yaoReadingCode/spring-boot-troubleshooting/commit/6260c394f4c28d155bbf016a758f3d37a5682a9c.

Accessing Tomcat context parameter in Camel REST dsl

I am currently working on a set of REST resources that are deployed in a Tomcat servlet container. I have been using the Camel REST dsl (kickstarted by Spring) and it works very well. Now, I would like to get access to some parameters specified in the container's context.xml but havn't been able to figure out how to do that as I do not have access to the Servlet context in my route builder. Any suggestions?
Even though you are hosting your Spring-app on Tomcat, Camel is using a separate, internally loaded web server implementation for it's REST endpoints, e.g. Restlet or Spark-rest (for more infos check here). For that reason your Camel route is ignorant and entirely disconnected to your Tomcat let alone its Servlets contexts.

Resources