使用restTemplate检索整数时出现问题



我目前正在学习spring-cloud微服务,下面是一个非常简单的例子。

因此,我有两个服务1用于currency exchange,1用于currency conversion。这两个服务使用"resttemplate"相互通信,但是,当我尝试从currency exchange服务获取对象时,我会获取除整数1之外的所有字段,这些字段返回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;
}

控制器:

CurrencyExchange控制器:

@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

**Web调试日志:

Exchange货币类别:

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=?
PrintCurrencyExchange{id=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 [CurrencyExchange{id=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显示CCD_ 6。帮我理解这一点。

谢谢

您需要添加一个@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不知道从哪一列加载数据,因为字段名与列名不匹配。

最新更新