使用 restTemplate 检索整数的问题
Posted
技术标签:
【中文标题】使用 restTemplate 检索整数的问题【英文标题】:Issue retrieving an integer using restTemplate 【发布时间】:2022-01-19 19:07:38 【问题描述】:我目前正在学习 Spring Cloud 微服务,我学习了一个非常简单的示例。
所以我有 2 个服务 1 用于 currency exchange
和 1 用于 currency conversion
。这两个服务使用“resttemplate”相互通信,但是,当我尝试从currency exchange
服务获取对象时,我得到了除整数之外的所有字段,这些字段返回0
。
豆类:
货币兑换:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CurrencyExchange
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "currency_from")
private String from;
@Column(name = "currency_to")
private String to;
private int convertionMultiple;
private String environment;
货币换算:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CurrencyConversion
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "from_currency")
private String from;
@Column(name = "to_currency")
private String to;
private int conversionMultiple;
private int quantity;
private int totalCalculatedAmount;
private String environment;
控制器:
货币兑换控制器:
@RestController
@RequestMapping("/exchange")
public class exchangeCurrencyController
@Autowired
private Environment environment;
@Autowired
private CurrencyExchangeService currencyExchangeService;
@GetMapping("/currency-exchange/from/from/to/to")
public CurrencyExchange exchangeCurrency(@PathVariable String from, @PathVariable String to)
CurrencyExchange currencyExchange = currencyExchangeService.findByFromAndTo(from, to);
String port = environment.getProperty("local.server.port");
System.out.println(currencyExchange);
currencyExchange.setEnvironment(port);
return currencyExchange;
货币转换控制器:
@RestController
@RequestMapping("/conversion")
public class CurrencyConversionController
@GetMapping("/currency-conversion/from/from/to/to/quantity/quantity")
public CurrencyConversion getCurrencyConversion(@PathVariable String from, @PathVariable String to, @PathVariable int quantity)
HashMap<String, String> uriVariables = new HashMap<>();
uriVariables.put("from", from);
uriVariables.put("to", to);
ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity("http://localhost:8000/exchange/currency-exchange/from/from/to/to", CurrencyConversion.class, uriVariables);
CurrencyConversion currencyConversion1 = responseEntity.getBody();
System.out.println(currencyConversion1);
return new CurrencyConversion(currencyConversion1.getId(), currencyConversion1.getFrom(), currencyConversion1.getTo(), currencyConversion1.getConversionMultiple(), quantity, currencyConversion1.getConversionMultiple(), currencyConversion1.getEnvironment());
我的数据库中有 3 个对象。
id conversion_multiple environment currency_from currency_to
1 12 8000 AUS ILS
2 65 8000 USD ILS
3 424 8000 INR ILS
**网页调试日志:
ExchangeCurrency 类:
2021-12-16 21:39:00.550 DEBUG 6648 --- [nio-8000-exec-3] o.s.web.servlet.DispatcherServlet : GET "/exchange/currency-exchange/from/USD/to/ILS", parameters=
2021-12-16 21:39:00.551 DEBUG 6648 --- [nio-8000-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.microservices.currencyexchangeservice.controllers.exchangeCurrencyController#exchangeCurrency(String, String)
2021-12-16 21:39:00.552 DEBUG 6648 --- [nio-8000-exec-3] org.hibernate.SQL : select currencyex0_.id as id1_0_, currencyex0_.conversion_multiple as conversi2_0_, currencyex0_.environment as environm3_0_, currencyex0_.currency_from as currency4_0_, currencyex0_.currency_to as currency5_0_ from currency_exchange currencyex0_ where currencyex0_.currency_from=? and currencyex0_.currency_to=?
2021-12-16 21:39:00.557 DEBUG 6648 --- [nio-8000-exec-3] org.hibernate.SQL : select currencyex0_.id as id1_0_, currencyex0_.conversion_multiple as conversi2_0_, currencyex0_.environment as environm3_0_, currencyex0_.currency_from as currency4_0_, currencyex0_.currency_to as currency5_0_ from currency_exchange currencyex0_ where currencyex0_.currency_from=? and currencyex0_.currency_to=?
PrintCurrencyExchangeid=2, from='USD', to='ILS', convertionMultiple=65, environment='8000'
2021-12-16 21:39:00.561 DEBUG 6648 --- [nio-8000-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2021-12-16 21:39:00.562 DEBUG 6648 --- [nio-8000-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [CurrencyExchangeid=2, from='USD', to='ILS', convertionMultiple=65, environment='8000']
2021-12-16 21:39:00.564 DEBUG 6648 --- [nio-8000-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
货币转换类**
2021-12-16 21:37:39.776 DEBUG 10736 --- [nio-8100-exec-9] o.s.web.servlet.DispatcherServlet : GET "/conversion/currency-conversion-feign/from/USD/to/ILS/quantity/1123123", parameters=
2021-12-16 21:37:39.777 DEBUG 10736 --- [nio-8100-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.microservices.currencyconversionservice.controllers.CurrencyConversionController#getCurrencyConversionFeign(String, String, int)
2021-12-16 21:37:39.795 DEBUG 10736 --- [nio-8100-exec-9] o.s.w.c.HttpMessageConverterExtractor : Reading to [com.in28minutes.microservices.currencyconversionservice.beans.CurrencyConversion]
CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=0, totalCalculatedAmount=0, environment=8000)
2021-12-16 21:37:39.798 DEBUG 10736 --- [nio-8100-exec-9] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
2021-12-16 21:37:39.798 DEBUG 10736 --- [nio-8100-exec-9] m.m.a.RequestResponseBodyMethodProcessor : Writing [CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=1123123, totalCalculatedAm (truncated)...]
2021-12-16 21:37:39.800 DEBUG 10736 --- [nio-8100-exec-9] o.s.web.servlet.DispatcherServlet : Completed 200 OK
当我打印以控制台响应时。我明白了
CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=0, totalCalculatedAmount=0, environment=8000)
它显示 String 返回但 int 显示 0
。
帮助我理解这一点。
谢谢
【问题讨论】:
数据库表是什么样子的? @JensBaitinger 我刚刚用数据库的结构编辑了我的帖子 休眠获取conversionMultiple 的值是否正确? @roma2341 显然它以0
的值获取它。我不知道为什么,因为其他字段都可以,都是strings
。也许它与映射有关?
可能是因为驼色
【参考方案1】:
你需要添加一个@Column
配置:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CurrencyExchange
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "currency_from")
private String from;
@Column(name = "currency_to")
private String to;
@Column(name = "conversion_multiple")
private int convertionMultiple;
private String environment;
否则 Spring 不知道从哪个列加载数据,因为字段名称与您的列名称不匹配。
【讨论】:
出于某种原因,我仍然在整数上使用0
。也许我的代码中的某些内容已损坏?
可能会激活debug log 并检查从数据库中加载了哪些数据以及它是如何映射到您的数据类的。以上是关于使用 restTemplate 检索整数的问题的主要内容,如果未能解决你的问题,请参考以下文章
为啥基本身份验证总是使用 restTemplate 给出错误 403?
使用 spring restTemplate 对 REST API 进行基本身份验证
如何获取通用地图作为来自 restTemplate 交换方法的响应?