Spring rest provides building pojo functionality by default from path variables and url parameters.
In my case I have pojo:
public class MyCriteria {
private String from;
private String till;
private Long communityNumber;
private String communityName;
}
that is used in my controller. Url is http://localhost:8080/community/{communityNumber}/app. Request result of
curl "http://localhost:8080/community/1/app?from=2018-11-14&till=2019-05-13&communityName=myCOm"
is:
{
'from':'2018-11-14';
'till':'2019-05-12';
'communityNumber':'1';
'communityName':'myCOm'
}
It seems works fine. Much more better to have in pojo data with required types by purposes. So I'd like to have fields from and till of LocalDate type. Using spring I'd like to have this solution almost out the box. But any spring or jackson date converters can't resolve my issue because of life cycle.
Spring validates types of pojo fields before injecting dates and I'm get type mismatch exception. I think the general reason is using special builder by spring that tries to find required parameters by name and it ignores annotations to be applied inside of pojo for the fields.
Is there any elegant solutions for building pojo by spring where some fields will be converted from String to LocalDate format by default?
Mandatory conditions are:
GET;public class MyCriteria {
private LocalDate from;
private LocalDate till;
private Long communityNumber;
private String communityName;
}
getters where injected convert logic;import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.vl.example.rest.dtofromoaramsandpath.web.dto.MyCriteria;
import java.time.LocalDate;
@RestController
@RequestMapping("verify/criteria/mapping")
@Slf4j
public class MyController {
@GetMapping("community/{communityNumber}/dto")
public MyCriteria loadDataByDto(MyCriteria criteria) {
log.info("received criteria: {}", criteria);
return criteria;
}
@GetMapping("community/{communityNumber}/default/params")
public String loadDataByDefaultParameters(@PathVariable("communityNumber") String communityNumber,
@RequestParam(value = "from", required = false) String from,
@RequestParam(value = "till", required = false) String till,
@RequestParam(value = "communityName", required = false) String communityName) {
log.info("received data without converting:\n\tcommunityNumber => {}\n\tfrom => {}\n\ttill => {}\n\tcommunityName => {}",
communityNumber, from, till, communityName);
return new StringBuilder("{")
.append("\n\tfrom:").append(from).append(";")
.append("\n\tfrom:").append(from).append(";")
.append("\n\ttill:").append(till).append(";")
.append("\n\tcommunityName:").append(communityName)
.append("\n}\n").toString();
}
@GetMapping("community/{communityNumber}/converted/params")
public String loadUsingConvertedParameters(@PathVariable("communityNumber") String communityNumber,
@RequestParam(value = "from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
@RequestParam("till") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate till,
@RequestParam(value = "communityName", required = false) String communityName) {
log.info("received data with LocalDate converting:\n\tcommunityNumber => {}\n\tfrom => {}\n\ttill => {}\n\tcommunityName => {}",
communityNumber, from, till, communityName);
return new StringBuilder("{")
.append("\n\tfrom:").append(from).append(";")
.append("\n\tfrom:").append(from).append(";")
.append("\n\ttill:").append(till).append(";")
.append("\n\tcommunityName:").append(communityName)
.append("\n}\n").toString();
}
}
controller and criteria to make your understanding and experiments more useful.This might help you. I have similar situation and I used this approach to convert the data into specific requirement.
public class MyCriteria {
public MyCriteria(LocalDate from, LocalDate till, Long communityNumber, String communityName){
// assignement of variables
}
private LocalDate from;
private LocalDate till;
private Long communityNumber;
private String communityName;
}
SO whenever you create a object from JSON, it will create it according to the requirement.
When I implemented this, I used ObjectMapper class of "Jackson" to do this thing. Hope you are uisng the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With