英文:
Java controller always return a 404 no matter what I do
问题
package com.example.accessingdatamysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
@Controller
@RequestMapping(path="/demo")
public class MainController {
@Autowired
private UserRepository userRepository;
@PostMapping(path="/add")
public @ResponseBody String addNewUser (@RequestParam String name, @RequestParam String email) {
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}
package com.braintobytes.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.braintobytes.data.repository.CurrencyRepository;
@RestController
@RequestMapping("/demo")
public class CurrencyController {
@Autowired
private CurrencyRepository currencyRepository;
@GetMapping("/currency")
public String getCurrency() {
return "currency";
}
}
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- ... Other configurations ... -->
<dependencies>
<!-- ... Other dependencies ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<!-- ... Other dependencies ... -->
</dependencies>
<!-- ... Other configurations ... -->
</project>
Please note that your issue might be due to various factors such as incorrect URL paths, incorrect database configurations, or other parts of your project setup. The code snippets provided above are based on the content you've shared, but debugging specific issues might require a deeper analysis of your entire project.
英文:
I was following an example from the spring boot documentation and the java controller always returns a 404 for some reason
This is what I tried
package com.example.accessingdatamysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@PostMapping(path="/add") // Map ONLY POST Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
which is the example given by spring boot here https://spring.io/guides/gs/accessing-data-mysql/
Then I also tried
@RestController
@RequestMapping("/demo")
public class GreetingClass {
@GetMapping("/greeting")
public String getGreeting() {
return "hello";
}
}
.pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.braintobytes</groupId>
<artifactId>Finance_microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Finance_microservice</name>
<description>Finance service</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I know this is weird, I set up everything as instructed on the page but apparently nothing works, and I also checked other questions similar to this none of them answer my question.
It does seem that the tomcat server is hit, because it says when I make a call
o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
but then I get with this call curl 'http://localhost:8080/demo/greeting' and with 'http://localhost:8080/demo/all'
curl : The remote server returned an error: (404) Not Found.
Logs (the file path is removed on the first line):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
2020-08-31 08:19:16.508 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : Starting FinanceMicroserviceApplication on DESKTOP-A65224I with PID 20708 ()
2020-08-31 08:19:16.510 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : No active profile set, falling back to default profiles: default
2020-08-31 08:19:17.096 INFO 20708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-31 08:19:17.101 INFO 20708 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-31 08:19:17.101 INFO 20708 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-31 08:19:17.150 INFO 20708 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-31 08:19:17.150 INFO 20708 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 618 ms
2020-08-31 08:19:17.465 INFO 20708 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-31 08:19:17.675 INFO 20708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-31 08:19:17.681 INFO 20708 --- [ main] c.b.f.FinanceMicroserviceApplication : Started FinanceMicroserviceApplication in 1.328 seconds (JVM running for 1.835)
2020-08-31 08:20:21.893 INFO 20708 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-31 08:20:21.893 INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-08-31 08:20:21.905 INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 12 ms
Response:
{
"timestamp": "2020-08-31T13:20:21.930+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/demo/greeting"
}
This works if I do this:
https://github.com/BraintoByte/Test_App_Stack/tree/master/demo
Different example:
I have attached a different example with hirarchy exactly the same but business logic omitted:
Code in controller:
package com.braintobytes.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.braintobytes.data.Currency;
import com.braintobytes.data.repository.CurrencyRepository;
@RestController
@RequestMapping("/demo")
public class CurrencyController {
@Autowired
private CurrencyRepository currencyRepository;
@GetMapping("/currency")
public String getCurrency() {
return "currency";
}
}
Project hierarchy:
Same exact pom and in application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
答案1
得分: 3
你的控制器所在的包是 - com.example.accessingdatamysql
Spring Boot 的启动日志显示你的主应用程序类 c.b.f.FinanceMicroserviceApplication 位于另一个无关的包层次结构中。
Spring Boot 建议将主应用程序类放在高于其他类的根包中,以便组件扫描可以顺利进行。
如果你将包 com.example 保持为根包,并将主应用程序类 FinanceMicroserviceApplication 移动到其中,那么一切都应该正常工作。
如果你不遵循 Spring Boot 对包层次结构的建议,那么你需要使用 @SpringBootApplication 注解显式指定要扫描的包。
英文:
Your controller is in package - com.example.accessingdatamysql
Spring boot startup log shows your main application class c.b.f.FinanceMicroserviceApplication is in another non related package hierarchy.
Spring boot recommends to locate main application class in a root package above other classes so that the component scan works out of box.
If you keep package com.example as root package and move your main application class FinanceMicroserviceApplication to it then everything should work as expected.
If you don't follow Spring boot recommendation for package hierarchy then you need to explicitly specify packages to scan with @SpringBootApplication annotation.
答案2
得分: 0
好的,以下是翻译好的部分:
好的,以上的建议都没有奏效,不过我们走在了正确的道路上。
将结构更改如下有效:
在 application.properties 中还存在路径问题,因此我进行了如下更改:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}
更改为:
jdbc:mysql://localhost
最后:
package com.braintobytes.finance.data.repository;
import org.springframework.data.repository.CrudRepository;
import com.braintobytes.finance.data.Currency;
public interface CurrencyRepository extends CrudRepository<Currency, Integer> {
}
哇!
英文:
Alright so none of the suggestions above worked, we were on the right path though.
Changing the structure as so worked:
There was also a pathing problem in the application.properties and thus I changed as so:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}
To:
jdbc:mysql://localhost
And finally:
package com.braintobytes.finance.data.repository;
import org.springframework.data.repository.CrudRepository;
import com.braintobytes.finance.data.Currency;
public interface CurrencyRepository extends CrudRepository<Currency, Integer> {
}
Woah!
答案3
得分: 0
对于任何遇到这个奇怪问题的人,只需将Spring Boot的主类移动到应用程序的主包中,类似于这样,我的主类名叫Demo2Application。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论