Reputation: 37
I've Initialized a SpringBoot Rest in eclipse and made it a Dynamic Web project. A 3-tire principle has been followed and endpoint URLs declared in controller classes. The project deploys fine but once I try to access an endpoint that returns a 404-error. Please refer to the example below. Used Compiler - Maven and Server - apache tomcat 9.0
MainClass.java
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
@Configuration
public class PmsAdvanced extends SpringBootServletInitializer implements
WebMvcConfigurer{
public static void main(String[] args) {
SpringApplication.run(PmsAdvanced.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Sample Controller class
@RestController
@EnableWebMvc
@RequestMapping("/test")
public class PmsAdvancedController implements WebMvcConfigurer {
@PostMapping("/PMS")
public String sayHello(@RequestParam(value = "myName", defaultValue = "divi") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/PMSA")
public String sayAge(@RequestParam(value = "age", defaultValue = "100") String age) {
return String.format("I'm %s!", age);
}}
URL I've been trying to access - http://localhost:8081/pms_advance_be/test/PMSA
Upvotes: 1
Views: 2644
Reputation: 2874
If you annotate something as @Configuration
then you are telling spring that the class is a source of @Beans
. You should therefore not have @RequestMapping
/@RestController
/@GetMapping
inside the same class. Separate the relevant parts for those items out into another class.
I'm also pretty sure for the set up you have you don't need to repeatedly add @EnableAutoConfiguration
and probably only need it (if at all) alongside the root @SpringBootApplication
annotation (possibly alongside any @SpringBootTest
as well but don't quote me on this). If you get rid of them and read any exceptions you get back, you should be in a better position to get this working.
In MVC style apps (regardless of language) the typical naming convention is that all requests are handled by classes named "BlahBlah...Controller", handle incoming HTTP requests.
So in summary, to get this working, I'd start with:
@EnableAutoConfiguration
PmsAdvancedController
to PmsAdvancedConfiguration
and keep method addViewControllers
within it only.@RestController
& @RequestMapping("/test")
from PmsAdvancedConfiguration
PmsAdvanceController
and add to it the annotations @RequestMapping("/test")
methods marked @GetMapping
and @PostMapping
@RestController
combines @Controller
and @ResponseBody
- it is not necessary. Delete.WebMvcConfigurer
but I am assuming you're following some sort of example judging by the "Hello Worldiness" of it... Looking at other examples online and the documentation it should work with the following two annotations: @EnableWebMvc
and @Configuration
all other annotations can be removed.EDIT:
If you drop the below into a single Java file and run the main
method it works:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class PmsAdvanced extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PmsAdvanced.class, args);
}
}
@Configuration
class PmsAdvancedConfiguration implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
@RestController
@RequestMapping("/test")
class PmsAdvancedController {
@PostMapping("/PMS")
public String sayHello(@RequestParam(value = "myName", defaultValue = "divi") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/PMSA")
public String sayAge(@RequestParam(value = "age", defaultValue = "100") String age) {
return String.format("I'm %s!", age);
}
}
TESTING:
curl -X GET http://localhost:8080/test/PMSA?age=30
Or visit in browser: http://localhost:8080/test/PMSA?age=30
Output:
I'm 30!
Note: I'm not actually 30.
Upvotes: 2