java.lang.numberformatexception错误:对于fetch to plain endpoint中的输入字符串

b4wnujal  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(389)

你好,开发人员,我试图处理这个问题,但找不到一个合理的解决方案。我用微服务做这个应用,其中最重要的项目(租客、产品、租金)都是一个微服务。由于这一点和它的相互依赖性,我还创建了一个公共库,其中重用几个应用程序组件所需的表是它的主要功能。在其中一个微服务控制器中,我通过获取所有产品的端点接收到此错误:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String]
 to type [java.lang.Long] for value 'all'; nested exception is java.lang.NumberFormatException: For input
 string: "all"

此错误的原因是产品的控制器中引用了以下内容:

package com.microproducts.controllers;

import com.commons.dtos.ProductsDtos;
import com.commons.entities.Product;
import com.microproducts.exceptions.GeneralException;
import com.microproducts.exceptions.NotFoundException;
import com.microproducts.responses.AppResponse;
import com.microproducts.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class ProductController {
    @Autowired
    ProductService productService;

    @Autowired
    ProductsDtos productsDtos;

 @GetMapping("/products/all")==============================>APARENTLY HERE
    public List<Product> getAllProducts() {
       List<Product> allProducts = productService.getAllProducts().stream().collect(Collectors.toList());
        return allProducts;
    }

}

note:i have 作为替代品,两者都不起作用

@GetMapping(value="/products/all")
...
 @GetMapping(path="/products/all")
...
 @ResponseStatus(HttpStatus.OK)
 @RequestMapping(value = "/products/all", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
...
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "products/all", method = RequestMethod.GET)

正如您在这个端点中看到的,我没有传递参数或可能导致奇怪行为的东西,因此仍然无法理解它。
然后,我引用此流程的服务和实现包得到了以下结构:

SERVICE
package com.microproducts.services;
import com.commons.entities.Product;
import java.util.List;
import java.util.Map;

public interface ProductService {

    List<Product> getAllProducts();

}

SRVICE IMPLEMENTACION
package com.microproducts.servicesImpl;

import com.commons.dtos.ProductsDtos;
import com.commons.entities.Product;
import com.microproducts.exceptions.GeneralException;
import com.microproducts.exceptions.NotFoundException;
import com.microproducts.repositories.ProductRepository;
import com.microproducts.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductRepository productRepository;

    @Autowired
    ProductsDtos productsDtos;

       @Override
    @Transactional(readOnly = true)
    public List<Product> getAllProducts() {
        List<Product> productsList = productRepository.findAll();
       return productsList;
    }
 }

产品的存储库和实体是这样设计的:

package com.microproducts.repositories;

import com.commons.entities.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

}

在本例中,由于产品实体是可重用的,因此我在一个名为commons的包中共享了它

package com.commons.entities;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name="PRODUCT")
public class Product implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    Long id;

    @Column(name="PRODUCT_NAME")
    private String productName;

    @OneToMany(fetch=FetchType.LAZY,mappedBy = "product")
    private Set<Rent> listRentsInProduct=new HashSet<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="RENTER_ID",nullable = false)
    private Renter renter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PRODUCT_TYPE_ID",nullable = false)
    private ProductType productType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PRODUCT_PRICE_ID",nullable = false)
    private ProductPrice productPrice;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PRODUCT_SUBTYPE_ID",nullable = false)
    private ProductSubType productSubType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PRODUCT_INVENTARY_ID",nullable = false)
    private ProductInventary productInventary;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PRODUCT_FEE_DELAY_ID",nullable = false)
    private ProductFeeDelay productFeeDelay;

    private static final long serialVersionUID=1285454306356845809L;

    /////////////////////////////constructor////////////////////////////
    public Product(){};
    public Product(Long id, String productName,
//                   Set<Rent> listRentsInProduct,
                   Renter renter, ProductType productType,ProductPrice productPrice,
                   ProductSubType productSubType, ProductInventary productInventary,
                   ProductFeeDelay productFeeDelay) {
        this.id = id;
        this.productName = productName;
        this.listRentsInProduct = listRentsInProduct;
        this.renter = renter;
        this.productType = productType;
        this.productPrice = productPrice;
        this.productSubType = productSubType;
        this.productInventary = productInventary;
        this.productFeeDelay = productFeeDelay;

    }

    /////////////////////////other methods/////////////////////////////////
    public void addRent(Rent rent){
        listRentsInProduct.add(rent);
    }
    public Set<Rent>getAllRentsOfUser(){return listRentsInProduct;}

    ///////////////////////////getters and setters////////////////////////////////////
    public Long getId() {  return id; }
    public void setId(Long id) {  this.id = id; }

    public String getProductName() {  return productName;}
    public void setProductName(String productName) {  this.productName = productName;}

    public Set<Rent> getListRentsInProduct() {  return listRentsInProduct; }
    public void setListRentsInProduct(Set<Rent> listRentsInProduct) { this.listRentsInProduct = listRentsInProduct; }

    public Renter getRenter() { return renter;}
    public void setRenter(Renter renter) {   this.renter = renter; }

    public ProductType getProductType() {   return productType; }
    public void setProductType(ProductType productType) {    this.productType = productType; }

    public ProductPrice getProductPrice() {   return productPrice;}
    public void setProductPrice(ProductPrice productPrice) {   this.productPrice = productPrice;}

    public ProductSubType getProductSubType() {    return productSubType;}
    public void setProductSubType(ProductSubType productSubType) {   this.productSubType = productSubType;}

    public ProductInventary getProductInventary() {   return productInventary;}
    public void setProductInventary(ProductInventary productInventary) {    this.productInventary = productInventary;}

    public ProductFeeDelay getProductFeeDelay() { return productFeeDelay; }
    public void setProductFeeDelay(ProductFeeDelay productFeeDelay) { this.productFeeDelay = productFeeDelay; }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", productName='" + productName + '\'' +
                ", listRentsInProduct=" + listRentsInProduct +
                ", renter=" + renter +
                ", productType=" + productType +
                ", productPrice=" + productPrice +
                ", productSubType=" + productSubType +
                ", productInventary=" + productInventary +
                ", productFeeDelay=" + productFeeDelay +
                '}';
    }
}

扫描所有可共享组件和实体的spring应用程序配置以这种方式公开:

package com.microproducts;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"com.commons.dtos","com.commons.entities","com.commons.exceptions","com.commons.responses"})
@EntityScan("com.commons.entities")
public class MicroproductsApplication {

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

}

尽管我的控制器的url出现了问题,但我还是想公开部分代码,原因可能是我的结构不正确,或者我遗漏了一些可能导致错误的内容。但是,很少见的是,试图获取我的数据时,在那个普通url字符串上出现了错误,这是由于某种后台进程假装将字符串或其中的一部分静音到很长。太好了!提前谢谢!!!。
顺便说一下,我对这个微服务的pom是:

<?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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.microproducts</groupId>
    <artifactId>microproducts</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microproducts</name>
    <description>Demo project for microproducts</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>

    <dependencies>
        <!--        LIBRERIA COMUN-->
        <dependency>
            <groupId>com.commons</groupId>
            <artifactId>commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制台中更详细的错误日志如下:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'all'; nested exception is java.lang.NumberFormatException: For input string: "all"
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:293) ~[spring-data-commons-2.3.6.RELEASE.jar:2.3.6.RELEASE]
    at org.springframework.data.repository.support.ReflectionRepositoryInvoker.invokeFindById(ReflectionRepositoryInvoker.java:145) ~[spring-data-commons-2.3.6.RELEASE.jar:2.3.6.RELEASE]
    at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindById(CrudRepositoryInvoker.java:92) ~[spring-data-commons-2.3.6.RELEASE.jar:2.3.6.RELEASE]
    at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindById(UnwrappingRepositoryInvokerFactory.java:94) ~[spring-data-rest-core-3.3.6.RELEASE.jar:3.3.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:514) ~[spring-data-rest-webmvc-3.3.6.RELEASE.jar:3.3.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:327) ~[spring-data-rest-webmvc-3.3.6.RELEASE.jar:3.3.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) [tomcat-embed-core-9.0.41.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.41.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]
Caused by: java.lang.NumberFormatException: For input string: "all"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_221]
    at java.lang.Long.parseLong(Long.java:589) ~[na:1.8.0_221]
    at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_221]
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:214) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:64) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:50) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:437) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    ... 58 common frames omitted

2020-12-19 10:42:06.389  WARN 7180 --- [nio-9000-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'all'; nested exception is java.lang.NumberFormatException: For input string: "all"]

这是我的数据库模式和产品表的初步视图

u91tlkcl

u91tlkcl1#

您在不同的微服务中使用同一个表。这表明您的微服务定义不正确。我建议您修改您的数据模型,检查哪些数据属于一个域,哪些属于不同的域。只有这样才能定义服务。然后你可能会看到2个甚至所有3个当前的微服务实际上应该是一个单一的微服务。然后检查持久性是如何工作的。
电话 CrudRepositoryInvoker.invokeFindById() 意味着有储存库方法 findById() 打电话。我们看不到控制器类的全部代码。但我想实际上有两种方法:

@GetMapping("/products/all")
public List<Product> getAllProducts() {
    ...

@GetMapping("/products/{id}")
public List<Product> getProductById(@PathVariable("id") Long id) {
    ...

打电话的时候 /products/all ,斯普林认为这是一种召唤 /products/{id} 并试图转换 all 一个长期的价值。因此,我们可以在op中看到堆栈跟踪。
原因是什么?原因是设计不好。方法 /products/all 使整个服务不休息。
解决方案?在这里:

@GetMapping("/products")
public List<Product> getAllProducts() {
    ...
jbose2ul

jbose2ul2#

显然,您的项目中已经激活了springbootstarter数据rest。有了这个库,rest控制器神奇地获得了一个特殊的“含义”,您的endpoint/products/all现在意味着“返回id为“all”的产品”。只要从pom.xml中删除这个依赖项,我想您不需要它。

xxb16uws

xxb16uws3#

请在控制器中添加替代项并尝试。

@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "products/all", method = RequestMethod.GET)
public List<Product> getAllProducts(HttpServletRequest request, HttpServletResponse response) {
   List<Product> allProducts = productService.getAllProducts().stream().collect(Collectors.toList());
    return allProducts;
}

我想你不需要补充 findAll() 在jpa存储库中,因为它有一个默认值。你可以评论一下。

相关问题