Ignore / from #requestmapping value in MVC Spring - spring

My file path or directory like a/b/c/d
I am triggered this in rest but i am getting HTTP Status 404 error.
i want to ignore / from filepath and want to print like a/b/c/d without 404 error.
#RequestMapping(value = "/TEST/{filepath:./*}", method = RequestMethod.GET)
public void DownloadFile(#PathVariable("filepath") String filePath) {
System.out.println(filePath);
}
This is what i am triggered in rest TEST/a/b/c/d
Please Help..

This solution does not use PathVariable:
#RequestMapping(value = "/TEST/**", method = RequestMethod.GET)
public void DownloadFile(HttpServletRequest request) {
String filePath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
System.out.println(filePath.substring(filePath.indexOf("TEST")));
}

Related

changing redirect: while returning modelandview

how to remove context path from redirect: while returning modelandview.
Mycurrent path looks like-- localhost.com:8080/main
where /main is my context path.
I want to redirect to localhost.com:8080/new
while returning modelandview using redirect:
or, You can say I want to one level up in my path.
NOTE: "localhost.com:8080" is environment specific so it changes accordingly.
my code:
Here this is part of controller class with request mapping value= /main.
#RequestMapping(value = "/{responsePath:[\\w-]+}" + UNCOMPR_SUFFIX + ".xml" //UNCOMPR_SUFFIX =-uncompressed
+ APACHE_URL_SUFFIX_FILTER, method = RequestMethod.GET)
public ModelAndView getUncompressed(//
#ModelAttribute(MODEL_NAME) final TaskModel model, //
#PathVariable(value = "responsePath") final String responsePath) {
final WebserviceLog task = this.getServiceFacade().getSystemServices().getWebserviceLogService()
.getByResponsePath(responsePath + FILE_SUFFIX_XML);
byte[] uncompressed = this.getServiceFacade().getSystemServices().getWebserviceLogService()
.getUncompressedResponse(task.getId());
} catch (Exception exception) {
return new ModelAndView(//
String.format("redirect:/new/%s", responsePath));
}
So, here instead of going to localhost:8080/main/new/xyz.xml
I want to go to localhost:8080/new/xyz.xml.

Rest API methods with same URL

I have 2 GET REST methods as given below in a class:
#RequestMapping(value = "test/server", method = {RequestMethod.GET})
public void getServer() {
....
}
#RequestMapping(value = "test/{key}", method = {RequestMethod.GET})
public void getTestPathVariable(#PathVariable("key") final String key) {
....
}
when I consume the rest api with URL "http://localhost:8080/test/server". It always calls the getServer() method.
I am wondering why it does not create an ambiquity as the URL is valid for both getServer() and getTestPathVariable() method. Please help me to understand.

Spring WebFlux + thymeleaf: Post request redirect Get page returns the 303 see other status

I just used SpringBoot + WebFlux + thymeleaf to write the controller.
#RequestMapping(value = "/create", method = RequestMethod.GET)
public String createCityForm(Model model) {
model.addAttribute("city", new City());
model.addAttribute("action", "create");
return CITY_FORM_PATH_NAME;
}
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String postCity(#ModelAttribute City city) {
cityService.saveCity(city);
return REDIRECT_TO_CITY_URL;
}
I witre thymeleaf page to receive the form, and redirect/return the get method page, But the browser give the 303 see other status.
Also, the delete resources also doesn't work.
The SEE_OTHER status is actually the default status of the RedirectView when invoked without explicitly specifying the HTTP code (like the ThymeleafReactiveViewResolver does).
If you want to override this status, return the RedirectView directly instead of letting Thymeleaf do it when it matches the redirect: pattern in the view name:
#RequestMapping(value = "/create", method = RequestMethod.GET)
public RedirectView createCityForm(Model model) {
model.addAttribute("city", new City());
model.addAttribute("action", "create");
return new RedirectView("/target_url", HttpStatus.MOVED_PERMANENTLY);
}

Different encoding of an HTTP request result, depending on the Accept header

I have a controller with a method to upload files, using, on the client side, the dojo Uploader class that supports ajax uploads for all browsers except IE, and uploads with an IFrame for IE.
The result is a JSON object, but when the IFrame mechanism is used, the JSON must be enclosed in a <textarea>:
#RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST)
#ResponseBody
public String uploadDocumentation(HttpServletRequest request,
#PathVariable String appId, #RequestParam("uploadedfile") MultipartFile file)
throws Exception {
// ....
String json = JsonUtils.jsonify(map);
if (accepts(request, "application/json")) {
return json;
} else if (accepts(request, "text/html")) {
return "<textarea>" + json + "</textarea>";
} else {
throw new GinaException("Type de retour non supporté");
}
I was wondering if there is a way to register this encoding mechanism in the framework, so that we would just have to return an object, and let the framework do the rest.
Thanks in advance.
For the record, I simply added a second method:
#RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST,
produces="application/json")
#ResponseBody
public UploadResult uploadDocumentation(#PathVariable String appId,
#RequestParam("uploadedfile") MultipartFile file) throws Exception {
...
return new UploadResult(filename);
}
#RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST,
produces="text/html")
#ResponseBody
public String uploadDocumentationIE(#PathVariable String appId,
#RequestParam("uploadedfile") MultipartFile file) throws Exception {
UploadResult obj = uploadDocumentation(appId, file);
String json = JsonUtils.jsonify(obj);
return "<textarea>" + json + "</textarea>";
}

Spring MVC + GWT : Redirect Issue

I am using Spring annotated MVC framework in an app which I am developing.
Following is the issue I am facing:
I have Controller which does a redirect, after a POST:
#RequestMapping(value = "/emdm-viewer-redirect.do", method = RequestMethod.POST)
public ModelAndView getMetricKeysAndRedirect(#RequestParam Object jsonObject, Model model)
{
ModelAndView modelAndView = new ModelAndView("redirect:/mdm-viewer.do");
.....
.....
....//make some service calls and populate value1
...
modelAndView.addobject("param1", value1);
return modelAndView;
}
I have another controller which is mapped to URL mdm-viewer.do (The redirect URL mentioned above):
#RequestMapping(value = "/mdm-viewer.do", method = RequestMethod.GET)
public String getMDMViewer(Model model) {
return "mdmViewer"; //returns a mdmViewer.jsp
}
Please note that the mdmviewer.jsp is a GWT entrypoint which is in classpath.
I have my firebug window open which tells me that a GET request was made for mdm-viewer.do, but it gives me a blank response. In fact, it does not redirect to the new jsp and stays on the same page from where the POST request was made.
However, if I copy the firebug URL and open it in a new window of my browser, I see the expected results.
Any ideas what I am doing wrong here? Tried to google it a lot, but can't find a similar issue anywhere.
Eventually, I returned a ModelAndView back from the POST method using a
#ResponseBody
And in my GWT Module, I used the response.getText() output to do a
#Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
JSONObject jsonObject = (JSONObject) JSONParser.parse(response.getText());
String viewName = jsonObject.get("viewName").isString().stringValue();
JSONObject jsonParams = jsonObject.get("model").isObject();
Set<String> chartKeys = jsonParams.keySet();
String redirectURL = viewName + "?";
for (String keyString : chartKeys) {
redirectURL = redirectURL + keyString + "=" + jsonParams.get(keyString).isString().stringValue() + "&";
}
Window.open(GWT.getHostPageBaseURL() + redirectURL, "_self", "");
}
}

Resources