Consider defining a bean of type 'org.springframework.social.twitter.api.Twitter' in your configuration - spring

So I am following https://spring.io/guides/gs/accessing-twitter/ to connect my application to Twitter to get some user data. But it looks like with recent changes to Spring boot (version 2.0.3.RELEASE) and Spring-social-twitter, I am not able to execute the said article successfully. I keep getting following error:
Consider defining a bean of type 'org.springframework.social.twitter.api.Twitter' in your configuration.
Has anyone tried the same sample with new Spring boot release? What am I missing?

Related

Open API (Swagger) non working in Spring Boot when adding context path

I have a Spring Boot application exposing REST services that are easily called on addresses like
http://localhost:8080/<controller_mapping>/<service_mapping>.
I've been asked to modify my settings in order to add a context path and have my services to respond on
http://localhost:8080//gesev-mensa/<controller_mapping>/<service_mapping>.
Thus I edited my application.properties adding
server.servlet.context-path=/gesev-mensa
Everything works but I can't call Swagger on old address
http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config#/
I get the error Failed to load remote configuration
As suggested, I tried to add property
springdoc.swagger-ui.path=/gesev-mensa/swagger-ui/index.html
but problem persists.
I guess Swagger should be reachable at
http://localhost:8080/gesev-mensa/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config#/
but that doesn't work.
Any hint?
Thanks for support.
Try removing
springdoc.swagger-ui.path=/gesev-mensa/swagger-ui/index.html
from your properties,
And your swagger will be available in
http://localhost:8080/gesev-mensa/swagger-ui/index.html
As per your current configuration with,
springdoc.swagger-ui.path=/gesev-mensa/swagger-ui/index.html
Swagger will be available in
http://localhost:8080/gesev-mensa/gesev-mensa/swagger-ui/index.html

Difference Between Spring MockMvcBuilders.standaloneSetup and WebMvcTest

I have a Spring Boot 2.6.0 application that depends on libraries which use fields initialised with SpEL syntax, e.g.
#Value("${some.value.in.library}") aField;
I attempted to write a Spring MVC test for a controller with #WebMvcTest. However I kept getting failed to load applicationcontext errors due to a SpELEvaluationException for the fields mentioned above.
Even after trying many, many, different test annotations such as #ConfigurationProperties and other fixes. I could see the properties that are defined in application-test.yml being loaded in the startup logs but they were not getting injected into the class.
Finally, I tried using:
MockMvcBuilders.standaloneSetup
and the test was able to start and run successfully.
Unfortunately, due to the complexity of the application and the use of a library where the problem occurs, it is difficult to create a minimal reproducable example.
So my question is, why does MockMvcBuilders.standaloneSetup work, whereas #MockMvcTest doesn't?
This answer suggests that #MockMvcTest should not load the full application context. But that does not seem to be the case based on the failed to load applicationcontext errors.

What changed about loading Spring Resource file from classpath in Spring Boot in v1.5.12?

Prior to Spring Boot v1.5.11, the following worked for referring to a file called auth.json in src/main/resources:
spring.cloud.gcp.credentials.location=auth.json
Starting with v1.5.11 and continuing through into 1.5.12, the above results in a FileNotFound exception. But the following works:
spring.cloud.gcp.credentials.location=classpath:auth.json
What changed in Spring Boot 1.5.12 (or its underlying dependencies) that was responsible for this?
This was caused by a fix to another issue and is being tracked via another reported instance here: https://github.com/spring-projects/spring-boot/issues/12786
Practitioners hitting this behavior may either update their implementation in an appropriate way (e.g., in our sanitized sample, it would be to prefix the property with "classpath:") or to wait to see if further resolution (that doesn't regress the other fix that caused this) is possible.
h/t #Stephane-Nicoll and Andy Wilkinson.

Facing issue while integrating CustomSpringUtilityJar in SpringBootApplication

I am having two spring applications and the details as follows:
1). Application One: This is a utility jar and its purpose is for CRUD operations on a specific table. We are using spring data jpa and hibernate for achieving the same. Since it is an utility this will not run alone
and it will be incuded as dependency to other applications. In this case we are using Application2 for that.
2) Application Two: This is a web service application made of Spring boot using yml configuration and also using Application One for CRUD operations on that table.
Issue: While running application Two(i.e. Spring Boot Application) , I am getting the error message related to the the components, repositories and entities of Application one,
as the bean is not registered in the configuration. Following are the approaches I used to solve thes:
Approach 1: In application 2, along with #SpringBootApplication annotation:
added Component Scan, Enity Scan and Enable JPA repositories and in these three annotations we have to include the package of both Application1 and Application2.
This scenario is working fine.
Note: I am not using Approach 1 as I have to make these three changes in every service applications which is going to use this utility jar. So decided to proceed with Approach2
Approach 2: Created an custom annotation like #UtilJar annotation and configuration class in Application1 and in that configuration class, added the three
annotations along with the configuration annotation.
In this case, while adding the custom annotation(created from Application1) in Application2, all the components and repositories registered in Application1 is registered and error is
showing related to the repositories registered in Application 2 . The only way I can solve on this is to give EnableJPARepository in Application2 which again similar to
Approach1.
So the root cause what I understood is because of #EnableJPARepository annotation given in the Application1 prompted for this error in Application2. Can anyone suggest me how to proceed on this.

How to load cache the data present in a table during server start up

I am developing a java web application in which I want to cache all the data present in a table during server start up.
Also if there are any changes in DB values, I wish to refresh the cache (without restarting the server).
I am looking for some material in spring which may help me in achieving that. But I am not able to figure it out.
Please help how can I achieve the same. Also I would like to initialize some beans on server start up.
To start with read the following docs which will get you started.
Refer to Spring document http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/cache.html
Also check this simple tutorial http://viralpatel.net/blogs/cache-support-spring-3-1-m1/
Regarding your bean initialization you can use #PostConstruct annotation on a method of the bean class. Spring will call that method after the bean is constructed.
The application that your planning to build wouldnt be an easy one. In my experience, creating an application like that would require a knowledge of the following:
1. Spring
2. Ehcache
3. JMX
4. Servlet Listeners

Resources