[英]Error 404 - Get request using @RestController - SpringBoot
Yes, there are lot of questions about it, but each case is unique. 是的,对此有很多疑问,但是每种情况都是唯一的。
The objective is write a simple application to make CRUD operations on a entity Product
, using: Controller
, Model
and Repository
. 目的是编写一个简单的应用程序,以使用:
Controller
, Model
和Repository
对实体Product
进行CRUD操作。
Tree: 树:
+- com.teste
+- controller
| +- ProductController.java
+- model
| +- Product.java
+- repository
| +- ProductRepository.java
+- SpringEsApplication.java
ProductController.java ProductController.java
@RestController
//@RequestMapping(value="/product") // When try it, not works too (same error).
public class ProductController {
@Autowired
private ProductRepository productRepository;
@PostMapping("/saveProduct")
public long saveProduct(@RequestBody List<Product> products) {
productRepository.saveAll(products);
return productRepository.count();
}
@GetMapping("/findAllProducts")
public Iterable<Product> findAllProducts() {
return productRepository.findAll();
}
@GetMapping("/findProductByCode")
public List<Product> findProductByCode(@PathVariable String code) {
return productRepository.findByCode(code);
}
}
Product.java 产品.java
@Document(indexName = "product_index")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
@Id
private String id;
private String name;
private String code;
private double price;
}
ProductRepository.java ProductRepository.java
@Repository
public interface ProductRepository extends CrudRepository<Product,String> {
List<Product> findByCode(String code);
}
SpringEsApplication.java SpringEsApplication.java
@SpringBootApplication
@ComponentScan(basePackages = {"com.teste.repository"})
public class SpringEsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEsApplication.class, args);
}
}
Postman GET Request 邮递员GET请求
GET Request: GET请求:
http://localhost:8080/findAllProducts
Response: 响应:
{
"timestamp": "2019-09-18T14:10:38.305+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/findAllProducts"
}
Even without data, it should return something. 即使没有数据,它也应该返回一些内容。
Console log starting 控制台日志启动
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-18 11:16:34.061 INFO 4764 --- [ restartedMain] com.teste.SpringEsApplication : Starting SpringEsApplication on CTDDELL5JVV862 with PID 4764 (started by augusto.cadini in C:\Users\augusto.cadini\Desktop\Spring ElasticSearch projects\spring-es)
2019-09-18 11:16:34.069 INFO 4764 --- [ restartedMain] com.teste.SpringEsApplication : No active profile set, falling back to default profiles: default
2019-09-18 11:16:34.193 INFO 4764 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-09-18 11:16:34.193 INFO 4764 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-09-18 11:16:34.774 INFO 4764 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-18 11:16:34.829 INFO 4764 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 51ms. Found 2 repository interfaces.
2019-09-18 11:16:35.516 INFO 4764 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-18 11:16:35.544 INFO 4764 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-18 11:16:35.544 INFO 4764 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-18 11:16:35.671 INFO 4764 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-18 11:16:35.672 INFO 4764 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1479 ms
2019-09-18 11:16:35.949 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : no modules loaded
2019-09-18 11:16:35.950 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2019-09-18 11:16:35.950 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2019-09-18 11:16:35.950 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2019-09-18 11:16:35.950 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2019-09-18 11:16:35.950 INFO 4764 --- [ restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019-09-18 11:16:36.901 INFO 4764 --- [ restartedMain] o.s.d.e.c.TransportClientFactoryBean : Adding transport node : 192.168.99.100:9300
2019-09-18 11:16:37.260 INFO 4764 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-09-18 11:16:37.572 INFO 4764 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-18 11:16:37.956 INFO 4764 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-18 11:16:37.957 INFO 4764 --- [ restartedMain] com.teste.SpringEsApplication : Started SpringEsApplication in 4.838 seconds (JVM running for 5.522)
Remove @ComponentScan(basePackages = {"com.teste.repository"})
from you main class. 从您的主类中删除
@ComponentScan(basePackages = {"com.teste.repository"})
。
It is not required in your case. 在您的情况下不是必需的。
When you provide @ComponentScan
, Spring engine will scan those packages only which you have provided. 当您提供
@ComponentScan
,Spring引擎将仅扫描您提供的那些软件包。
@ComponentScan
is required to provide custom scanning of packages/classes. 需要
@ComponentScan
提供包/类的自定义扫描。
The Person identifier is of type Long, so repository should be extends CrudRepository<Product, Long>
not CrudRepository<Product, String>
. Person标识符的类型为Long,因此存储库应扩展为
CrudRepository<Product, Long>
而不是CrudRepository<Product, String>
。
Next, rest constroller is outside of "com.teste.repository"
package. 接下来,rest constroller在
"com.teste.repository"
包的外部。 You can remove @ComponentScan
annotation and spring will find controller. 您可以删除
@ComponentScan
批注,spring会找到控制器。
The problem is with your @ComponentScan
. 问题出在您的
@ComponentScan
。 By default Spring boot scans all packacges starting from the location of your SpringApplication.java
. 默认情况下,Spring Boot会从SpringApplication.java的位置开始扫描所有
SpringApplication.java
。 You have now overridden this and asking the container to scan only repository
. 现在,您已经覆盖了此要求,并要求容器仅扫描
repository
。 Remove this line. 删除此行。 Instead, use
@EnableMongoRepositories
而是使用
@EnableMongoRepositories
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.