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:
- remove all lines starting/equal to
@EnableAutoConfiguration
- rename
PmsAdvancedController to PmsAdvancedConfiguration and keep method addViewControllers within it only.
- remove
@RestController & @RequestMapping("/test") from PmsAdvancedConfiguration
- create a new class
PmsAdvanceController and add to it the annotations @RequestMapping("/test") methods marked @GetMapping and @PostMapping
@RestController combines @Controller and @ResponseBody - it is not necessary. Delete.
- I can't see what you're attempting to do with the
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.