Spring Boot 404 Error with Simple Controller - spring

I am trying to make a simple Spring Boot Application with the simple controller (Restful controller) where the function is returning the String that is to be printed on the Web page. But I am facing a problem where I am getting 404 error every time. In my views, I am setting up the application right. I have just used spring-boot-starter-web as the dependency.
Application file:
Any help would be appreciated.
And I know #SpringBootApplication automatically take all the annotation.

Either your controller class needs to be in or sub package of SpringBoot Application class or the package in which controller class exists needs to be scanned explicitly using #ComponentScan to resolve this

Can you show me the structure of your project?, I believe the problem Spring is not finding your userController, #SpringBooApplication use the #ComponentScan how is in charge to search for all the spring annotations un your code, so I believe is something

Need to add #ComponenScan the resquest to http:localhost:8080/api should return "Hello" if you need to add a path http:localhost:8080/user/api do as follow:
#RestController
#RequestMapping(value = "/user")

Related

How configure Spring Boot to show a custom #Endpoint in the base path '/actuator' but alphabetically?

In Spring Boot 2.4.1. For a custom #Endpoint I have the following:
#Component
#Endpoint(id="springhost")
class SpringHostEndpoint {
#ReadOperation()
HostInfo report() {
...
}
}
It works fine, the custom #Endpoint appears under the .../actuator base path how /springhost as follows:
From above it is the second link or item.
The situation is that it is not located in the right place such as all the rest of endpoints, it according alphabetically
How configure Spring Boot to accomplish that automatically?
Spring Boot does not sort the links returned by the /actuator endpoint. While in such cases it is preferable that sorting is done on the client side, I've raised an issue to see if this is something we want to consider doing in a future release.

How to add rest controller to spring boot library, and use it in another application?

I am working on a library xyz.jar that needs to add a UI page with mappings like this one:
#RestController
public class LibCtrl {
#EventListener(ApplicationReadyEvent.class)
#RequestMapping("/updateDomainList")
String updateDomainList() {
return "we can call a controller from another jar like this";
}
}
This then needs to be called in my main springboot application, myMainApplication.war, so when I call
http://localhost/myMainApplication/updateDomainList
I should see
we can call controller from another jar like this
on the browser.
How can achieve this? #Component also did not work for me. Once it begins to work, would #Autowired to JdbcTemplate also work?
It was a simple fix. #ComponentScan allows for multiple packages to be scanned. This made is possible for me to add my Library Packages to be managed by Spring. Just add the following to your application class.
#ComponentScan({"my.mainapplication.package","my.library.package"})

Springboot build not working because of test configuration

I have started a spring boot project using start.spring.io.
But I am getting this error-
I have read various articles on the internet about this issue and they all say about putting my tests in the same package as my Main class.
But I already have the same.
Could you point out what is wrong with my configuration?
The exception is pretty clear: You are missing a configuration for your spring context. What you need to do is to add the configuration classes for your context like so:
#SpringBootTest(classes = { TestConfiguration.class })
whereas your TestConfiguration class must be annotated with
#Configuration
and/or
#EnableAutoConfiguration
There you can add configurations to your liking. You can of course also use your DatabaseApplication class as Configuration although Im wouldn't recommend that.
The search algorithm works up from the package that contains the test until it finds a #SpringBootApplication or #SpringBootConfiguration annotated class. As long as you’ve structure your code in a sensible way your main configuration is usually found.
Make Sure your DatabaseApplication class is annotated with #SpringBootApplication .

How to override #RestController bean in springboot

I have a #RestController annotated class defined in a 3rd party .jar file in my springboot app. It's initialized via package scan. How can I overwrite this 3rd party controller bean with my own controller bean? Is there something like the "alias" of the xml configuration in spring-boot with java configure only environment?
Each mapping must be unique.There is no way to overrule an existing #RequestMapping.
There are workarounds for this though.
you can refer this post to get an idea about the answer :
How to override #RequestMapping in another controller?

base package attribute in context:component-scan tag in spring 3

I am writing simple Hello application using maven in spring 3. I have made a HelloWorldService class by using #Service annotation. In the applicaioncontext.xml file giving different value to base-package attribute of context:component-scan base-package="yyy.xxx". My program is running.
What is the use of base-package in context:component-scan?
What is the purpose of the second tag, <context:component-scan>? Well, you need a bit of background information to understand the purpose of this tag.
Basically,say the #Controller annotation indicates that a particular class located at base-package="yyy.xxx" serves the role of a controller. Another example the #RequestMapping annotated methods to serve a web request.
So, component-scan base-package="yyy.xxx" will tell the spring container via the dispatcher servlet to locate such annotated classes and methods for mapping. But, There are plenty of more detailed explanation if you google it.

Resources