【问题标题】:Nullpointer Exception unknown resource springmicriserviceNullpointerexception 未知来源spring微服务
【发布时间】:2020-11-07 13:08:58
【问题描述】:

cnv 任何人帮助我我不知道在哪里出错,当我运行我的 键入时在浏览器中的 chrome 应用程序 http://localhost:8100/currency-converter-feign/from/USD/to/INR/quantity/1000 我遇到了这种类型的错误

这个应用程序没有明确的 /error 映射,所以你看到 这是一个后备。

IST 2020 年 7 月 17 日星期五 21:38:29 出现意外错误 (类型=内部服务器错误,状态=500)。没有可用的消息 java.lang.NullPointerException 在 java.math.BigDecimal.multiply(未知来源)在 com.main.conteoller.CurrencyconversionController.convertCurrencyFfeign(CurrencyconversionController.java:47) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:215) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:142) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 在

currencyconvertercontroller.java

package com.main.conteoller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.main.Bean.ConversionBean;
import com.main.serviceproxy.CurrencyexchangeserviceProxy;

@RestController
public class CurrencyconversionController {
    
    @Autowired
    private CurrencyexchangeserviceProxy proxy;
    
    @GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
    public ConversionBean convertCurency(@PathVariable String from,
            @PathVariable String to,
            @PathVariable BigDecimal quantity) {
        
        Map<String, String>uriVariables= new HashMap<>(); 
        uriVariables.put("from", from);
        uriVariables.put("to", to);
        ResponseEntity<ConversionBean>resp = new RestTemplate()
                .getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
                ConversionBean.class,
                uriVariables);
               ConversionBean cbresp=resp.getBody();
        return new ConversionBean(cbresp.getId(),from,to,cbresp.getConvermultiple(),
                        quantity,quantity.multiply(cbresp.getConvermultiple()),cbresp.getPort());
        
    }
    @GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
    public ConversionBean convertCurencyFfeign(@PathVariable String from,
            @PathVariable String to,
            @PathVariable BigDecimal quantity) {
        
               ConversionBean cbresp=proxy.retrivefromexchange(from, to);
        return new ConversionBean(cbresp.getId(),from,to,cbresp.getConvermultiple(),
                        quantity,quantity.multiply(cbresp.getConvermultiple()),cbresp.getPort());
        
    }
}

currencybean.java

package com.main.Bean;

import java.math.BigDecimal;

public class ConversionBean {
    private int id;
    private String from;
    private String to;
    private BigDecimal convermultiple;
    private BigDecimal quantity;
    private BigDecimal totalCalamount;
    private int port;
    
    public ConversionBean() {}
    
    public ConversionBean(int id, String from, String to, BigDecimal convermultiple, BigDecimal quantity,
            BigDecimal totalCalamount, int port) {
        super();
        this.id = id;
        this.from = from;
        this.to = to;
        this.convermultiple = convermultiple;
        this.quantity = quantity;
        this.totalCalamount = totalCalamount;
        this.port = port;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    public BigDecimal getConvermultiple() {
        return convermultiple;
    }
    public void setConvermultiple(BigDecimal convermultiple) {
        this.convermultiple = convermultiple;
    }
    public BigDecimal getQuantity() {
        return quantity;
    }
    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
    public BigDecimal getTotalCalamount() {
        return totalCalamount;
    }
    public void setTotalCalamount(BigDecimal totalCalamount) {
        this.totalCalamount = totalCalamount;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    

}

currencyexchangeproxy.java

package com.main.serviceproxy;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import com.main.Bean.ConversionBean;

@FeignClient(name="currency-exchange",url="localhost:8000")

public interface CurrencyexchangeserviceProxy {
    @GetMapping("/currency-exchange/from/{from}/to/{to}")
    public ConversionBean retrivefromexchange(@PathVariable ("from") String from,
            @PathVariable ("to") String to);

}

【问题讨论】:

  • 您的项目中是否有@EnableFeignClients?如果不是,请将其放到您的应用程序类中,然后重试。
  • @EnableFeignClients 它在我的 SpringBootApplication 下面的 main 方法中声明

标签: spring-boot microservices


【解决方案1】:

这是由于您的 db 表中的 comumn 名称不匹配而发生的 你的实体类。因为我使用微服务,所以我打电话给一些交易所 通过在我的存储库中提供另一个项目的 url,在 该项目我在我的实体类和我的数据库表中使用相同的名称 ,但我在我的交换服务项目中声明了不同的名称。我在那里 声明 conversationmultople 并在此处声明 convermultiple .确保在 db 和您的 bean 类中的名称相同

【讨论】:

    【解决方案2】:

    你的springboot主方法中的@EnableFeignClient服务

    例如

    包 com.main;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients("com.main.serviceproxy")
    public class CurrencyConversionServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CurrencyConversionServiceApplication.class, args);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多