【发布时间】: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 方法中声明