Not all endpoints exposed, despite from the same controller - spring-boot

I have the following controller in my Spring Boot application :
#RestController
#RequestMapping(value = "/users")
public class UserController {
#Autowired
UserService userService;
#GetMapping(value ="/helloWorld")
public String getHelloWorld() {
return "Hello World!";
}
#GetMapping(value = "/getAll")
public #ResponseBody
Iterable<User> getAllInvestors() {
return userService.getAllUsers();
}
}
When I make an HTTP Get on http://127.0.0.1:5000/users/getAll, it works perfectly : I get all the users from the database...
but when I make a call on http://127.0.0.1:5000/users/helloWorld, I get an unexpected error (type=Not Found, status=404)
PS 1 : When I call http://127.0.0.1:5000/api-docs to get the API definition : Both endpoints are exposed.
PS 2 : I've already made a Maven Clean, restarted IntelliJ, deleted all cookies from the browser.
PS 3 : No errors during compilation.

The issue was the case sensitivity, it was solved when I replaced #GetMapping(value ="/helloWorld") with #GetMapping(value ="/helloworld")
Refer to this topic for further details

Related

Properties in RestController is not accessible and Null in Junit

When i run my junit for Rest Controller class, properties in Rest controller is throwing Nullpointer exception. Below is my sample code. when i run the testFileStatus() method of TestingControllerTest calass, i am getting Nullpointer exception. Kindly help to resolve the issue. Any suggestion why i can't able to access the property even though it is available in test application.properties.
enter image description here
#RestController
#CrossOrigin(origins = "*", allowedHeaders = "*")
public class TestingController {
#Value("${file.dir.status}")
private String fileDirectoryStatus;
#GetMapping(value="/filedetails")
public String getFileDetails(.....){
if(fileDirectoryStatus.equalsIgnoreCase("true")) { //NullpointerException in Junit test
//code to process file
}
}
}
src/main/resources/application.properties:
file.dir.status=false
src/test/resources/application.properties:
file.dir.status=false
#SpringBootTest
#TestPropertySource(
locations = "classpath:resources/application.properties")
public class TestingControllerTest{
#InjectMocks
private TestingController testingController;
#Test
public void testFileStatus() {
String status=testingController.getFileDetails(...);
}
}

I get 404 error when I try endpoint on localhost

When I try GET on localhost:8080/path I get 404 error.
endpoint:
#Path("path")
#Produces(MediaType.TEXT_PLAIN)
public Controller {
#GET
public String getString() {
return "hello";
}
}
project structure looks like this:
com.example.demo.controller.Controller.java
com.example.demo.DemoApplication.java
Project is generated with Spring Initializr with spring web and lombok and nothing else.
You are using JAX-RS with Spring Boot. You must use Spring MVC
#RestController
#RequestMapping("/path")
public Controller {
#GetMapping
public String getString() {
return "hello";
}
}
You should start with reading the documentation:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-developing-web-applications
https://spring.io/guides/gs/rest-service/

#RequestMapping not working in Spring Boot

Controller class.
#RestController
#RequestMapping("/check")
public class Controller {
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
Application class
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Added all the necessary dependency when running the application shows the
http://localhost:8081/demo/
Hello World
of index.xml
When I change to http://localhost:8081/check/ it gives
HTTP Status 404 – Not Found
Type Status Report
Message /check
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
How can I understand the flow of Spring Boot application?
You need to put the Http method on your method, here I am assuming you are doing a GET request
#RestController
#RequestMapping("/check")
public class Controller {
#GetMapping // you forgot to put http method here
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
Note: GetMapping is only available if you are using Spring 4.3 or above else use #RequestMapping(value = "/url", method = RequestMethod.GET)
Your controller should be like this:
#RestController
public class Controller {
#RequestMapping(value="/check")
public String index(){
return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}
}
It seems
#RequestMapping(value="/check") is not working.
switch to
#RequestMapping(path="/check")
though as per documentation it should work.

Springboot #Controller cannot be invoked, but #RestController works

My controller has been annotated with #Controller and it cannot be invoked
- The browser shows
There was an unexpected error (type=Not Found, status=404).
But, if it is annotated with #RestController, then it works. My SpringBoot version:1.5.3.RELEASE
My Controller : (in com.sbootsecurityjsp.controller)
#Controller
public class LoginController {
#RequestMapping(value = "/login", method= RequestMethod.GET )
public String login() {
return "Login Controller";
}
}
Main Class: (in com.sbootsecurityjsp)
#SpringBootApplication(scanBasePackages = {"com.sbootsecurityjsp"})
public class SbootSecurityJspApplication {
public static void main(String[] args) {
SpringApplication.run(SbootSecurityJspApplication.class, args);
}
}
I am curious why the #Controller cannot work if the #RestController annotation works. If component scan is not working, #RestController also should not work. I have added scanbasePackages too. Even without scanbasePackages, it does not work.
By the way, when the app starts, the logs also show a line as following:
INFO 532 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.sbootsecurityjsp.controller.LoginController.login()
Why #Controller is used is to differentiate requests to pages and rest calls. Please correct me if I am wrong. My idea is to use #RestController s for REST requests, on the other hand #Controller for pages related requests- redirecting to JSP or any logics related to views. Is it a bad practice ?
Why does it return a 404 when I use #Controller annotation?
When using #Controller, Spring expects the String you return in #RequestMapping methods to correspond to the page you want to redirect the user to.
#RequestMapping(value = "/login", method= RequestMethod.GET )
public String login() {
return "Login Controller";
}
Here, Spring will try to redirect the user to Login Controller.jsp, which cannot be found and thus returns a 404.
Why does it not return 404 when I use #RestController
When using #RestController, the String you return is not mapped to any page. Instead, Spring just transforms it to e.g. a JSON response. This is why this doesn't give you a 404.
Proposed solution
If you have a jsp page called login.jsp, simply return "login":
#RequestMapping(value = "/login", method= RequestMethod.GET )
public String login() {
return "login";
}

Spring boot #GetMapping for root not working

My Spring Boot controller is working just fine, except that I cannot create a mapping for the home directory. I have tried:
#Controller
public class MyController {
#GetMapping(value = {"/"})
public ModelAndView searchPage(Locale locale) {
ModelAndView model = new ModelAndView();
model.setViewName("pageTemplate");
return model;
}
}
#GetMapping(value = "/")
#GetMapping(value = "")
#GetMapping
#RequestMapping with all values above
I always get my 404 error page. If this is supposed to work, how can I debug why it is not?
Found it. I set in application.properties:
logging.level.org.springframework.web: DEBUG
which then displayed
o.s.w.s.mvc.ServletForwardingController : Forwarded to servlet [springVaadinServlet] in ServletForwardingController 'vaadinUiForwardingController'
It turns out that I have used #SpringUI without specifying a path

Resources