Java - Pass multiple parameters to ajax post - java

I have this simple controller from ajax request. It works but I wanted to return many stuff, not only List TestFlow.getFlow(flowName);
#RequestMapping(value = "/execute-flow/getFlow" , method = RequestMethod.POST)
public #ResponseBody List<String> getFlow(#RequestParam("flowName") String flowName) {
return TestFlow.getFlow(flowName);
}
Can I return multiple things to the ajax post?
For example:
#RequestMapping(value = "/execute-flow/getFlow" , method = RequestMethod.POST)
public #ResponseBody List<String> getFlow(#RequestParam("flowName") String flowName) {
return TestFlow.getFlow(flowName);
return TestFlow.getInputs(flowName);
return TestFlow.getCode(flowName);
}

Not sure what exactly you're after, but
return Arrays.asList(
TestFlow.getFlow(flowName),
TestFlow.getInputs(flowName),
TestFlow.getCode(flowName));
should work, provided that all of these are of the same type (String).

Related

AWS lambda/api gateway does not redirect

I have a very simple spring route that im attempting to run on aws lambda. The route simply returns the text/string "redirect:/upload" instead of redirecting. I have the html file in the /resources/templates folder.
#RequestMapping(path = "/test", method = RequestMethod.POST)
public String UploadPage2() {
return "redirect:/upload";
}
I think the problem is from the return type of method: String.
You can do:
public RedirectView UploadPage2() {
return new RedirectView("/upload");
}
Second question
To return an view on path /test with GET request, you need another method with same path but different method
#RequestMapping(path = "/test", method = RequestMethod.GET)
public ModelAndView testGet(){
return new ModelAndView("uploadview");
}

Multiple #RequestMapping (value, method pairs) in Spring MVC

I would like to achieve something like this with Spring MVC
#RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
#RequestMapping(value = "/user/{userId}/delete", method = RequestMethod.POST)
public void deleteUser(#PathVariable String userId) {
...
}
This would give me a common endpoint for REST calls and standard HTML form posts.
Is it possible to do with Spring MVC?
All I can come up with is
#RequestMapping(value = { "/user/{userId}", "/user/{userId}/delete"}, method = {RequestMethod.DELETE, RequestMethod.POST})
public void deleteUser(#PathVariable String userId) {
...
}
but the result is slightly different because a POST to "/user/{userId}" would also delete the user.
One thing you could do is make 2 separate methods with their own RequestMapping annotation, and then just pass the parameters on to a different method, where you do actual stuff:
#RequestMapping(value = "/user/{userId}/delete", method = RequestMethod.POST)
public void deleteUserPost(#PathVariable String userId) {
deleteUser(userId);
}
#RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
public void deleteUserDelete(#PathVariable String userId) {
deleteUser(userId);
}
private void deleteUser(String userId){
//Do things here
}
Sorry, got it wrong way around.
In mature REST architecture, code should use a URL to refer to a resource and use HTTP method to define the action on the resource. So just define a #RequestMapping("/user/{userId}/delete", method = RequestMethod.DELETE) and eliminate the POST. See DELETE vs POST.

How to change url after spring controller invocation?

I have approximately following controllers:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public String foo(RedirectAttributes redirectAttributes, Model model) {
//logic
return bar(model);
}
#RequestMapping(value = "bar", method = RequestMethod.GET)
public String bar (Model model) {
model.addAttribute("value","magicValue")
return "myJsp";
}
my aim that after /foo invocation url was changed with bar
method bar shouldn't be broken.
Is it possible?
In method foo you can do:
return new ModelAndView("redirect:bar", modelName, model);
But this will add additional request to a server. It will also put the model values in the URL which is not always desirable

Use of getFlashAttributes() in Spring's RedirectAttributes

In order to access the redirect attributes in the redirected method, we utilize the model's map, like this :
#Controller
#RequestMapping("/foo")
public class FooController {
#RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model map) {
String some = (String) map.asMap().get("some");
}
#RequestMapping(value = "/bar", method = RequestMethod.POST)
public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
redirectAttrs.addFlashAttributes("some", "thing");
return new ModelAndView().setViewName("redirect:/foo/bar");
}
}
But, why can't we access them in this way :
#RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(RedirectAttributes redAttr) {
String some = redAttr.getFlashAttributes().get("some");
}
If the only purpose of adding flashAttributes is that they become available to the model in the redirected method, what's the purpose of getFlashAttributes() ?
RedirectAttributes are for setting flash attributes before redirection. They are merged into model after the redirection so there is no reason to access them again via RedirectAttributes again as you have suggested.
Being able to work with the attributes just like with a map might be useful. You can check what have you set (containsKey, isEmpty, ...). However the use of the wildcard generic parameter Map<String, ?> getFlashAttributes() prevents writing into map and it is strange why they have used it instead of a plain Object parameter.

.getJSON 404 Error with ModelAndView

I am trying to retrieve some JSON data in my javascript by making a call to the controller. The controller returns a MappingJacksonJsonView ModelandView, but the .getJSON is always reporting a 404 at .../handhygiene.json.
Is there a problem with the way I am returning the ModelandView from the controller?
Controller
#RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public ModelAndView getHandHygienePageAsync(
#PathVariable(value = "room") String roomCode) {
ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
mav.getModelMap().addAttribute(blahblahblah); //adds some attributes
...
return mav;
}
Javascript
var currentURL = window.location;
$.getJSON(currentURL + ".json",
function(data) {
... //does stuff with data
}
If you're trying to get only an JSON object from Ajax request, you need to add #ResponseBody to your method, and make you result object as return from your method.
The #ResponseBody tells to Spring that he need to serialize your object to return to the client as content-type. By default, Spring uses JSON Serialization. ModelAndView will try to return an .JSP page. Maybe you don't have this jsp page on your resources so, the server return 404 error.
I Think this code should work for you:
#RequestMapping(value = "/{room}/handhygiene.json", method = RequestMethod.GET)
public #ResponseBody Room getHandHygienePageAsync(#PathVariable(value = "room") String roomCode) {
Room room = myService.findRoomByRoomCode(roomCode);
return room;
}
I'm assuming you're using the Room as your result object, but it may be another object or ArrayList, for example.
You cant take a look here for Spring example, and here for example and configuration.

Categories