Springboot #RequestParam parameter value from possible values - spring

I am using #RequestParam to get the column name against which I want to sort my results.
public ResponseEntity<Map<String, Object>> method(#PathVariable("tenant_id") Integer tenantId,
#PathVariable("asset_location_id") Integer assetLocationId,
#RequestParam(defaultValue = "") String search_keyword,
#RequestParam(defaultValue = "0") int page,
#RequestParam(defaultValue = "10") int size,
#RequestParam(defaultValue = "asset_name") String sort_field,
#RequestParam(defaultValue = "asc") String sort_dir) {}
How I can make sure sort_field has the only possible column names which are possible in a given response?

Related

startDate comes before endDate in swagger Is there any way to preserve the order, or even specify the order

Actual Result: endDate comes first then startDate comes next.
Expected Result: startDate comes first then endDate.
Is there any way to specify the requestParameter order?
This is the Actual Result
public ResponseEntity<ByteArrayResource> getDetails(
#NotEmpty(message = UIMessages.MUST_NOT_BE_EMPTY) #RequestParam(
value = "startDate") final String startDate,
#NotEmpty(message = UIMessages.MUST_NOT_BE_EMPTY) #RequestParam(
value = "endDate") final String endDate,
final HttpServletRequest request) throws IOException {
//statements
}
This is the swagger UI actual result

springboot keeps coming to an null value

public Account(BaseEntityBuilder<?, ?> b, String email, String name, String nickname, String pwd, UserRole usertype,
String userf1, String userf2, String userf3, String userf4, String userf5, String filetype) {
super(b);
this.email = email;
this.name = name;
this.nickname = nickname;
this.pwd = pwd;
this.usertype = usertype;
this.userf1 = userf1;
this.userf2 = userf2;
this.userf3 = userf3;
this.userf4 = userf4;
this.userf5 = userf5;
this.filetype = filetype;
}
code
public interface SpringReadFileService {
List<Account> findAll();
boolean saveDataFromUploadfile(MultipartFile file);
}
code
List<Account> users = springReadFileService.findAll();
for(int i = 0; i< users.size(); ++i) {
String a = users.get(i).getEmail();
}
code
In this way, I'm trying to get only all email value among the values in the account array, but it keeps coming to an empty value. Can't I bring it like this? I want to get the value of all the emails in the data.
This is how I understand your question:
You are using findAll of a Spring Data JPA repository in order to load all Account instances.
You are only interested in the non empty email addresses.
Define your repository as
class AccountRepository<Account, Long> { // replace Long with the type of the primary key
#Query("SELECT DISTINCT email FROM Account WHERE email IS NOT NULL")
String findAllEmails();
}
Then use findAllEmails to get the required result.
There are a couple of variants of the JPQL statement depending on what you want:
If you want duplicates or email is unique through some constraint drop the DISTINCT
If you also have empty but not null emails, i.e. emails of length 0 you might want to add AND email <> '' to the query.

set call limitation for some service

i am working on a spring boot project and i want ti set limitation for some service to call.for example i want in 1 minute,5 request send to this endpoint.
#GetMapping(path = "/allJobsByChartCode/{chartCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Page<?>> getAllOCMJobsByChartCode(#PathVariable("chartCode") String chartCode, #RequestParam int page, #RequestParam int size, #RequestParam(required = false, name = "orderby") String orderby, #RequestParam(required = false, name = "direction") String direction) {
Page<OCMJobsDTO> gridData = ocmJobsService.getAllOCmJobsByChartCode(chartCode, PageRequest.of(page, size), orderby, direction);
if (gridData.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(gridData, HttpStatus.OK);
}

How to hide a session parameters in Swagger with Springfox

#ApiOperation(value = "获取打卡信息", notes = "获取打卡信息")
#RequestMapping(method = RequestMethod.GET, value = "/{mPhone}/{mPassword}/{date}")
#ApiImplicitParams({
#ApiImplicitParam(name = "mPhone", value = "手机号", required = true, dataType = "String",defaultValue="13268690268",paramType="Path"),
#ApiImplicitParam(name = "mPassword", value = "密码", required = true, dataType = "String",defaultValue="111111",paramType="Path"),
#ApiImplicitParam(name = "date", value = "日期", required = true, dataType = "String",defaultValue="2017-07-04",paramType="Path"),
#ApiImplicitParam(name = "httpSession", value = "Session", required = false)})
public #ResponseBody String getSignInfo(#PathVariable String mPhone, #PathVariable String mPassword,
#PathVariable String date,
HttpSession httpSession) {
.......
}
enter image description here
I want to remove this parameter (httpSession) from the document, and I need help。
Springfox won't show these values by default. The reason why httpSession is visible in your case, is because you added it by yourself as an implicit parameter:
#ApiImplicitParam(name = "httpSession", value = "Session", required = false)
If you don't want the httpSession to pop up, remove that from your implicit parameters. Additionally, you don't even have to use #ApiImplicitParam in your case, you can use #ApiParam:
#ApiOperation(value = "获取打卡信息", notes = "获取打卡信息")
#RequestMapping(method = RequestMethod.GET, value = "/{mPhone}/{mPassword}/{date}")
public #ResponseBody String getSignInfo(
#ApiParam(value = "手机号", required = true, dataType = "String",defaultValue="13268690268")
#PathVariable String mPhone,
#ApiParam(value = "密码", required = true, dataType = "String",defaultValue="111111")
#PathVariable String mPassword,
#ApiParam(value = "日期", required = true, dataType = "String",defaultValue="2017-07-04")
#PathVariable String date,
HttpSession httpSession) {
// ...
}

How to give default date values in requestparam in spring

#RequestMapping(value = "/getSettlements", method = RequestMethod.GET, headers = "Accept=application/json")
public #ResponseBody
Collection<Settlement> getSettlements
(#RequestParam(value = "startDate") String startDate,
#RequestParam(value = "endDate") String endDate,
#RequestParam(value = "merchantIds", defaultValue = "null") String merchantIds)
How to give today's date in defaultValue ? It only takes constant.
#InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
final CustomDateEditor dateEditor = new CustomDateEditor(df, true) {
#Override
public void setAsText(String text) throws IllegalArgumentException {
if ("today".equals(text)) {
setValue(new Date());
} else {
super.setAsText(text);
}
}
};
binder.registerCustomEditor(Date.class, dateEditor);
}
#RequestParam(required = false, defaultValue = "today") Date startDate
If you are using LocalDate, you can create a default value like this:
#RequestParam(name = "d", defaultValue = "#{T(java.time.LocalDate).now()}", required = true) LocalDate d)
I tried pretty much every option, even using interceptors. But from far the easiest solution was to use SpEL. For Example: defaultValue = "#{new java.util.Date()}"
Since you receive a string you can any date format you want and later on use formatting to extract the date

Resources