How to hide a session parameters in Swagger with Springfox - spring-boot

#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) {
// ...
}

Related

Trying to send Http response to the frontend before method logic happens

What I am trying to accomplish is I have a controller that gets accessed from a frontend (Angular). The users uploads an array of images from the frontend and those images are sent and processed through the backend (Spring Boot). Before the images are processed, I would like to send a response (200) to the frontend so the user does not have to wait for the images to be processed. The code looks like so:
#CrossOrigin
#RestController
public class SolarController {
#Autowired
SolarImageServiceImpl solarImageService;
#Autowired
SolarVideoServiceImpl solarVideoService;
#ApiOperation(value = "Submit images")
#PostMapping(value="/solarImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void getUploadImages(#ApiParam(value = "Upload images", required = true) #RequestPart(value = "files") MultipartFile[] files,
#ApiParam(value = "User's LanId", required = true) #RequestParam(value = "lanID") String lanId,
#ApiParam(value = "Site name", required = true) #RequestParam(value = "siteName") String siteName,
#ApiParam(value = "User email", required = true) #RequestParam(value = "userEmail") String userEmail,
#ApiParam(value = "Inspection ID", required = true) #RequestParam(value = "inspectionID") String inspectionID) throws IOException{
if (!ArrayUtils.isEmpty(files)) {
this.solarImageService.uploadImages(files, lanId, siteName, userEmail, inspectionID);
}
I have looked at multiple other examples, as in using #Async over the method, using HttpServletResponse, and setting my own responses. But nothing is working.
Resolved.
#ApiOperation(value = "Submit images")
#PostMapping(value="/solarImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void getUploadImages(#ApiParam(value = "Upload images", required = true) #RequestPart(value = "files") MultipartFile[] files,
#ApiParam(value = "User's LanId", required = true) #RequestParam(value = "lanID") String lanId,
#ApiParam(value = "Site name", required = true) #RequestParam(value = "siteName") String siteName,
#ApiParam(value = "User email", required = true) #RequestParam(value = "userEmail") String userEmail,
#ApiParam(value = "Inspection ID", required = true) #RequestParam(value = "inspectionID") String inspectionID, HttpServletResponse response) throws IOException{
int code = (!ArrayUtils.isEmpty(files)) ? HttpServletResponse.SC_OK
: HttpServletResponse.SC_NOT_FOUND;
if (code != HttpServletResponse.SC_OK) {
response.sendError(code);
return;
}
PrintWriter wr = response.getWriter();
response.setStatus(code);
wr.flush();
wr.close();
if (!ArrayUtils.isEmpty(files)) {
this.solarImageService.uploadImages(files, lanId, siteName, userEmail, inspectionID);
}
Sending the HttpServletResponse first did the trick. Annotating the method with #Async did not work.

How to customized and Override Parameter Values from POJO?

I am working on Spring Boot v2.2.6.RELEASE and Open API Integration example. This example has capability to search using 20 different parameters. So this POJO class holds CustomSearchDto these 20 different values.
In the POJO I used orgName, but #parameter(in = ParameterIn.QUERY, name = "orgizationName", and somehow I wanted to override the variable name. I must do that. Is there any way to do it ?
#Parameter(in = ParameterIn.QUERY, name = "orgizationName", schema = #Schema(type = "string"))
#Parameter(in = ParameterIn.QUERY, name = "employeeId", schema = #Schema(type = "string"))
#Parameter(in = ParameterIn.QUERY, name = "emailId", schema = #Schema(type = "string"))
#Parameter(in=ParameterIn.QUERY, name="page", description="Results page you want to retrieve (0..N)", schema=#Schema(defaultValue = "0"))
#Parameter(in=ParameterIn.QUERY, name="size", description="Number of records per page.", schema=#Schema(defaultValue = "30"))
#GetMapping(value = "/employees/organizations")
public ResponseEntity<PagedModel<Employees>> search(CustomSearchDto requestparams,
#Parameter(hidden=true) Pageable pageRequest) {
......
........
return new ResponseEntity<>(model, HttpStatus.OK);
}
Here is my custom DTO class
public class CustomSearchDto {
#Schema(description = "", type = "string", example = " ")
private String orgName;
#Schema(description = "", type = "string", example = " ")
private String empId;
#Schema(description = "", type = "integer", example = "null")
private Integer email;
.........
..............
.............
}
You can pass directly you object CustomSearchDto with the annotation #ParameterObject.
Here is the link for the documentation:
https://springdoc.org/faq.html#how-can-i-extract-fields-from-parameter-object-

Spring API #RequestParam Input conversion

I am creating a method for an API. In this method i have some parameters that are optional. those are filters for searching an event. when i try to run it and type the following url:
http://localhost:8181/api/events?id=gRDHzDh9TdiLDAZgrZc2wg==
i get this error message:
Failed to convert value of type 'java.lang.String' to required type 'java.util.UUID'; nested exception is java.lang.IllegalArgumentException: Invalid UUID string: gRDHzDh9TdiLDAZgrZc2wg==
So i understand that i insert a String in my url and expect a UUID in code, but how do i convert this? Below here is my code:
#RequestMapping(
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity getEvents(#RequestParam(value = "id", required = false) UUID eventId,
#RequestParam(value = "title", required = false) String title,
#RequestParam(value = "playtype", required = false) PlayType playType,
#RequestParam(value = "skilllevel", required = false) SkillLevel skillLevel,
#RequestParam(value = "sporttype", required = false) SportType sportType,
#RequestParam(value = "long", required = false) String _long,
#RequestParam(value = "lat", required = false) String lat) {
try {
List<Event> events = eventService.getEvents(eventId, title, playType, skillLevel, sportType, _long, lat);
if (events.size() == 0) {
return new ResponseEntity("No events found", HttpStatus.OK);
}
return new ResponseEntity(events, HttpStatus.OK);
} catch (Exception ex){
return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
So here are my 2 questions:
How do i convert the string to a valid UUID input in the RequestParam?
How do i convert the string to a valid enum in the RequestParam? (because with the enums i have the same error)
EDIT
my code is now like this:
#RequestMapping(
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity getEvents(#RequestParam(value = "id", required = false) String eventId,
#RequestParam(value = "title", required = false) String title,
#RequestParam(value = "playtype", required = false) String playType,
#RequestParam(value = "skilllevel", required = false) String skillLevel,
#RequestParam(value = "sporttype", required = false) String sportType,
#RequestParam(value = "long", required = false) String _long,
#RequestParam(value = "lat", required = false) String lat) {
UUID id = null;
PlayType playType1 = null;
SkillLevel skillLevel1 = null;
SportType sportType1 = null;
try {
if (eventId != null){
id = UUID.fromString(eventId);
}
if (playType != null){
playType1 = PlayType.valueOf(playType);
}
if (skillLevel != null){
skillLevel1 = SkillLevel.valueOf(skillLevel);
}
if (sportType != null){
sportType1 = SportType.valueOf(sportType);
}
List<Event> events = eventService.getEvents(id, title, playType1, skillLevel1, sportType1, _long, lat);
if (events.size() == 0) {
return new ResponseEntity("No events found", HttpStatus.OK);
}
return new ResponseEntity(events, HttpStatus.OK);
} catch (Exception ex){
return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}
but i still get an error:
Invalid UUID string: gRDHzDh9TdiLDAZgrZc2wg==
How do I convert the string to a valid UUID input?
You need to use UUID.fromString() API, see here
How do i convert the string to a valid enum?
You need to use Enum.valueOf() API, see here

How to provide default values for array parameters in spring MVC url mapping?

#RequestMapping(value = "/getUserScoreCardDetails", method = RequestMethod.GET)
public #ResponseBody List<ScoreDetails> getUserScoreCardDetails(
#RequestParam(value = "playerIds", required = false) int[] playerIds) {
}
I need to provide default values [1,2,3] for playerIds if playerIds is not available in request?
You can set comma separated values inside defaultValue property in #RequestParam
#RequestMapping(value = "/getUserScoreCardDetails", method = RequestMethod.GET)
public #ResponseBody List<ScoreDetails> getUserScoreCardDetails(
#RequestParam(value = "playerIds", required = false, defaultValue="1,2,3") int[] playerIds) {
}
Inside your method, just check, if playerIds is null and if it is null then specify the default values there like this
#RequestMapping(value = "/getUserScoreCardDetails", method =
RequestMethod.GET)
public #ResponseBody List<ScoreDetails> getUserScoreCardDetails(
#RequestParam(value = "playerIds", required = false) int[] playerIds) {
if(playerIds==null){
playerIds = {1,2,3};
}
}

How to redirect to a new url on the basis of a parameter in Spring

My RequestMapping & ResponseBody is like this:
#RequestMapping(value = "/someURL", method = RequestMethod.GET, produces = "application/json")
#ResponseBody
public ModelAndView getDetails(
#RequestParam(value = "param1", required = false) String param1,
#RequestParam(value = "param2", required = false) String param2,
{
Now on the basis of param1, I want to redirect to a particular URL.
Eg. Right now after processing, my page is going to
http://parent-domain/someURL?param1=...param2=...
But what I want is to completely change the parent domain like this
http://some-other-domain/someURL?param1=...param2=...
Do something like this:
#RequestMapping(value = "/someURL", method = RequestMethod.GET, produces = "application/json")
public ModelAndView getDetails(#RequestParam(value = "param1", required = false) String param1,
#RequestParam(value = "param2", required = false) String param2) {
if (param1.equals("a"))
return new ModelAndView("redirect:somotherurl");
else
return new ModelAndView("redirect:http://google.com");
}
if param1 is a then it will go to someotherurl in parent else it will redirect to google.com

Resources