Java:映射并设置为大写
在我的spring boot应用程序下,我有一个restfull服务方法:Java:映射并设置为大写,java,spring,rest,spring-boot,Java,Spring,Rest,Spring Boot,在我的spring boot应用程序下,我有一个restfull服务方法: @RequestMapping(value = "objects/{objectId}", method = RequestMethod.GET) public HttpEntity<ObjectDTO > getStoreById(@PathVariable("objectId") String storeId) throws DomainResourceNotFoundException {
@RequestMapping(value = "objects/{objectId}", method = RequestMethod.GET)
public HttpEntity<ObjectDTO > getStoreById(@PathVariable("objectId") String storeId) throws DomainResourceNotFoundException {
Object obj= ObjService.getObjById(objectId).orElseThrow(
() -> new DomainResourceNotFoundException(Store.class.getSimpleName(), objectId));
ObjectDTO objDTO = CustomMapperFactory.getMapper().map(obj, ObjectDTO .class);
return new ResponseEntity<>(objDTO , HttpStatus.OK);
}
我的目的是在映射“属性名称”时将其转换为大写,以产生以下格式:
"attributeone": "aaa"
"attributetWO": "bbb"
"ATTRIBUTEONE": "aaa"
"ATTRIBUTEOTWO": "bbb"
建议?JSON键反映StoreDTO中的属性名称。根据用于序列化的库,可以使用
@JSONProperty
覆盖属性名称(com.fasterxml.jackson.annotation.JSONProperty
for jackson,最常用):
如果要对所有属性执行此操作,可以使用以下方法:
objectMapper.setPropertyNamingStrategy(
new PropertyNamingStrategy.UpperCamelCaseStrategy()
)
但是结果将是
ATTRIBUTEONE
,而不是ATTRIBUTEONE
。如果确实需要ATTRIBUTEONE
,您可以实现自己的策略。JSON键反映StoreDTO中的属性名称。根据用于序列化的库,您可以使用@JSONProperty覆盖属性名称代码>(com.fasterxml.jackson.annotation.JsonProperty
for jackson,这是最常用的):
如果要对所有属性执行此操作,可以使用以下方法:
objectMapper.setPropertyNamingStrategy(
new PropertyNamingStrategy.UpperCamelCaseStrategy()
)
但结果将是ATTRIBUTEONE
,而不是ATTRIBUTEONE
。如果您确实需要ATTRIBUTEONE
,您可以实施自己的策略。实现这一点的一般方法是扩展并使用您自己的属性命名策略,如:
@SuppressWarnings("serial")
PropertyNamingStrategy pns = new PropertyNamingStrategy.PropertyNamingStrategyBase() {
@Override
public String translate(String propertyName) {
return propertyName.toUpperCase();
}
};
ObjectMapper om = new ObjectMapper();
om.setPropertyNamingStrategy(pns);
实现这一点的一般方法是扩展并使用您自己的属性名称策略
,如:
@SuppressWarnings("serial")
PropertyNamingStrategy pns = new PropertyNamingStrategy.PropertyNamingStrategyBase() {
@Override
public String translate(String propertyName) {
return propertyName.toUpperCase();
}
};
ObjectMapper om = new ObjectMapper();
om.setPropertyNamingStrategy(pns);
你的StoreDTO有小写字段名?你能显示它吗?你的StoreDTO有小写字段名吗?你能显示它吗?