I have set up my request controller and rest mapping but I am receiving 'Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Mar 11 20:28:19 GMT 2022 There was an unexpected error (type=Not Found, status=404). No message available' when I use the url http://localhost:8080/hello .
It was working a week ago. I have the correct dependency and I have tried component scan base package in my app.java and moving the controller to the same package as the app.java. Ive attached my package explorer and below is my code
package com.fyp.reviewchecker.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SearchController {
@RequestMapping("/hello") public String hello() {
return "Hello\n";
}
}
package com.fyp.reviewchecker;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
@EnableJpaAuditing
public class ReviewcheckerApplication implements CommandLineRunner {
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(ReviewcheckerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String sql = "INSERT INTO contact_table (name, subject, message, email, phone) VALUES ( ?, ?, ?, ?, ? ) ";
int result = jdbcTemplate.update(sql, "kirsty", "Complaint", "Love it", "[email protected]", "07429");
if (result > 0) {
System.out.println("contact form updated");
}
}
}
CodePudding user response:
first avoid using @RequestMapping use @PostMapping, @GetMapping etc instead.
check if you have configuration in your property files like this:
server.servlet.context
to check if the controller is being scanned by spring or not, you can try using applicationContext.getBeansOfType(SearchController.class); in your main/commandLineRunner or by using a @PostConstruct in the SearchController with some logging in the console.
CodePudding user response:
It was nothing to do with my code I had a 'missing' Jar file that I hadn't disposed of properly so my project wouldn't build until I had it. I added the jar file back and deleted it and now it works.