簡體   English   中英

錯誤404-使用@RestController獲取請求-SpringBoot

[英]Error 404 - Get request using @RestController - SpringBoot

是的,對此有很多疑問,但是每種情況都是唯一的。

目的是編寫一個簡單的應用程序,以使用: ControllerModelRepository對實體Product進行CRUD操作。

樹:

+- com.teste
  +- controller
  | +- ProductController.java
  +- model
  | +- Product.java
  +- repository
  | +- ProductRepository.java
  +- SpringEsApplication.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);
}

}

產品.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

@Repository
public interface ProductRepository extends CrudRepository<Product,String> {

    List<Product> findByCode(String code);

}

SpringEsApplication.java

@SpringBootApplication
@ComponentScan(basePackages = {"com.teste.repository"})
public class SpringEsApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringEsApplication.class, args);
    }

}

郵遞員GET請求

GET請求:

http://localhost:8080/findAllProducts

響應:

{
    "timestamp": "2019-09-18T14:10:38.305+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/findAllProducts"
}

即使沒有數據,它也應該返回一些內容。

控制台日志啟動

 :: 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)

從您的主類中刪除@ComponentScan(basePackages = {"com.teste.repository"})

在您的情況下不是必需的。

當您提供@ComponentScan ,Spring引擎將僅掃描您提供的那些軟件包。

需要@ComponentScan提供包/類的自定義掃描。

Person標識符的類型為Long,因此存儲庫應擴展為CrudRepository<Product, Long>而不是CrudRepository<Product, String>

接下來,rest constroller在"com.teste.repository"包的外部。 您可以刪除@ComponentScan批注,spring會找到控制器。

問題出在您的@ComponentScan 默認情況下,Spring Boot會從SpringApplication.java的位置開始掃描所有SpringApplication.java 現在,您已經覆蓋了此要求,並要求容器僅掃描repository 刪除此行。 而是使用@EnableMongoRepositories

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM