1

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 enter image description here

10
  • Please show the URL you calling (how it looks like) Commented Oct 23, 2020 at 7:45
  • Also how do you start deploy your app (as you say Tomcat I guess you are not running the main in the Spring Boot app) Commented Oct 23, 2020 at 7:46
  • @Dumbo - updated the content, sir. TIA Commented Oct 23, 2020 at 7:51
  • @derkoe - I use eclipse IDE configurations to deploy and run the project, sir. TIA Commented Oct 23, 2020 at 7:52
  • Seems like your app is not deployed or did not start properly in Tomcat - check the server logs (if you are using an external Tomcat) Commented Oct 23, 2020 at 8:04

1 Answer 1

2

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:

  1. remove all lines starting/equal to @EnableAutoConfiguration
  2. rename PmsAdvancedController to PmsAdvancedConfiguration and keep method addViewControllers within it only.
  3. remove @RestController & @RequestMapping("/test") from PmsAdvancedConfiguration
  4. create a new class PmsAdvanceController and add to it the annotations @RequestMapping("/test") methods marked @GetMapping and @PostMapping
  5. @RestController combines @Controller and @ResponseBody - it is not necessary. Delete.
  6. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess there are some good hints in your post but it does not answer the question asked.
True enough - I saw misconfigured/redundant code and went straight to fixing it! I'd suggest also checking you've got the right URL path. The path looks (on the face of it) to be more along the lines of GET http://host:port/test/PMSA but the path in use is: GET http://host:port/pms_advance_be/test/PMSA If OP can confirm and let us know what the result is that would be helpful
thanks for your time but even after configuring as you mentioned the problem still remains @RobEvans
are you able to provide more details... updated code, exception(s) your getting, stack trace too, any other relevant parts of code/config you're trying. I will try getting it working my end which I have not done yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.