corrupt url for spring handler - spring

i have such redirect within my javascript:
window.location.href = '/webapp/record.action?date='+varDate+'&id=' +
varId;
When i execute this, i invoke my spring-handler as expected:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="varId",) String id){...}
But my second parameter "varId" is everytime null. When i'am looking on my HttpServletRequest i see instead of shown url this url:
/webapp/record.action?varDate=2017-07-01&_=1500358872039)#1495183143
org.eclipse.jetty.server.Request#591eaf27
How this url has been created? Why i lost second parameter "varId" ?

you are passing the parameter as id and in spring controller you are trying to retrieve it as varId.
change you code in controller as below:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="id",) String id){
// your custom logic
}

Related

Angular 5: Sending POST request with string parameters and FormData parameter together, to a Spring REST Controller

I want to send a POST request from my service to a SpringBoot #RestController. I have a bunch of string parameters that I am sending, but I also have a FormData parameter which is an image (picture argument). If I do it like this:
public createEvent(name, description, fromDate, toDate, userId, picture){
this.http.post(this.baseUrl + 'create',
{
name: name,
description: description,
fromYear: fromDate['year'],
fromMonth: fromDate['month'],
fromDay: fromDate['day'],
toYear: toDate['year'],
toMonth: toDate['month'],
toDay: toDate['day'],
userId: userId,
picture: picture
}).subscribe();
}
And my Controller method looks like this:
#PostMapping(value = "/create")
public void createEvent(#RequestBody Map map){}
The map looks like this:
and I can't get the file.
I can send the FormData as a single parameter in a post request and receive it as a Multipart file in my controller without any problems, but is it possible to send it in the same request with the other parameters?
Apparently, you can append all of the parameters in the FormData object, and access them through #RequestParam in the controller.

Web API 2 attribute routing returning 404

I'm having trouble getting the Web API 2 attribute routing to work.
I've been trying everything I could find this whole evening but I can't find the problem.
What I want to achieve is the following:
Make a POST request to http://localhost:xxxx/api/chat/joingroup/1234 to get to the following API call:
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
This keeps getting me a http 400 message.
{"message":"No HTTP resource was found that matches the request URI 'http://localhost:41021/api/chat/joingroup/123'.",
"messageDetail":"No action was found on the controller 'Chat' that matches the request."}
But sending a post to: http://localhost:41021/api/chat/sendmessage/pm/123123 and also to http://localhost:41021/api/chat/joingroup gives me a 200
The chatcontroller:
[RoutePrefix("api/chat")]
public class ChatController : ApiController
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
[...]
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
HTTP POSTS to http://localhost:xxxx/api/chat/sendmessage are working fine.
I cannot figure out why it isn't going to the correct method when I'm calling a POST on http://localhost:xxxx/api/chat/joingroup/1234.
SOLUTION:
The solution was to reference both values that are needed in the JoinGroup method, id and connectionID. Now the request will hit this method.
Using:
http://localhost:xxxx/api/chat/joingroup/john?connectionid=123 will work.
I noticed two things on the code you sent through:
the path you POST to is: localhost:xxxx/joingroup/1234 , this
should be localhost:xxxx/api/chat/joingroup/1234
because you have 2 parameters for the joingroup, you will need to pass both of them through, may be like this localhost:xxxx/api/chat/joingroup/1234?connectionID=value or you can pass it on the request body
if the connectionID is optional you can modify the method to use option al parameters like this
public string JoinGroup(string id, string connectionID = "")
please let me know if this helps.
Thanks
Ashraf
I assume the connectionID parameter references the POSTed data. The easiest thing to make it work is to decorate it with the [FromBody] attribute and put an = in front of the value being sent like this: =MyConnection1.
Web API expects an object with properties or an array otherwise. Alternatively, you can wrap the connection ID with a custom class and pass it serialized as JSON/XML.

Spring controller, why is the returned view ignored?

So, say I have an existing, working page Display Cashier, which displays information about a cashier in a shop. Now, I add a button to this page that looks like:
Manager
The request-mapping for this URL maps it (successfully) to a controller: HandleGetManager
the HandleGetManager controller looks like this:
#Controller
public class HandleGetManager{
private employeeBO employeeBO; //BO handles all business logic
//spring hooks
public HandleGetManager(){}
public void setemployeeBo(employeeBO employeeBO){
this.employeeBO = employeeBO;
}
//get controller
#RequestMapping(method=RequestMethod.GET)
public String getManager(#RequestParam String cashierId){
Long managerId = employeeBO.getManagerByCashierId(cashierId);
String redirectUrl = "/displayManager.ctl?managerId=" + managerId.toString();
return redirectUrl;
}
}
Here's what happens when I try it:
I hit the new button on the Display Cashier page, I expect the following to happen:
The browser sends a get request to the indicated URL
The spring request-mapping ensures that the flow of control is passed to this class.
the #RequestMapping(method=RequestMethod.GET) piece ensures that this method is evoked
The #RequestParam String cashierId instructs Spring to parse the URL and pass the cashierId value into this method as a parameter.
The EmployeeBo has been injected into the controller via spring.
The Business logic takes place, envoking the BO and the managerId var is populated with the correct value.
The method returns the name of a different view, with a new managerId URL arg appended
Now, up until this point, everything goes to plan. What I expect to happen next is:
the browsers is directed to that URL
whereupon it will send a get request to that url,
the whole process will start again in another controller, with a different URL and a different URL arg.
instead what happens is:
this controller returns the name of a different view
The browser is redirected to a half-right, half wrong URL: handleGetManager.ctl?managerId=12345
The URL argument changes, but the name of the controller does not, despite my explicitly returning it
I get an error
What am I doing wrong? Have I missed something?
Assuming you have a UrlBasedViewResolver in your MVC configuration, the String value you return is a View name. The ViewResolver will take that name and try to resolve a View for it.
What you seem to want to do is to have a 301 response with a redirect. With view names, you do that by specifying a redirect: prefix in your view name. It's described in the documentation, here.
Here's a question/answer explaining all the (default) ways you can perform a redirect:
How can I prevent Spring MVC from doing a redirect?

Pass URL containing a query string as a parameter ASP.Net Web API GET?

I'm trying to pass in an URL as a string parameter to a WEB API GET method.
The controller:
public class LinksController : ApiController
{
public HttpResponseMessage Get(string targetUrl)
{
//query db with targetURL
}
}
The aim is to query the database to see if the URL is stored. This works fine with simple URLs and URLs whose query string contains a single parameter, like:
http://www.youtube.com/watch?v=nLPE4vhSBx4
The problem I'm encountering is specifically when the query string contains multiple parameters, e.g.
http://www.youtube.com/watch?v=nLPE4vhSBx4&feature=youtube_gdata
When debugging, the value of targetUrl is only ".../watch?v=nLPE4vhSBx4" which means &feature=youtube_gdata is lost.
The GET request looks like this:
http://localhost:58056/api/links?targetUrl=http://www.youtube.com/watch? v=nLPE4vhSBx4&feature=youtube_gdata
I've also tried to add the following route in WebApiConfig.cs:
config.Routes.MapHttpRoute(
name: "Links",
routeTemplate: "api/links/{targetUrl}",
defaults: new { controller = "Links", targetUrl= RouteParameter.Optional }
);
But the GET request then results in 400 Bad Request.
So my question is, can't this be done? I would like the complete URL! Or would I need to change the method header to use the [FromBody] attribute and pass it as a JSON object?
You should URLEncode your target URL parameter so that it doesn't get mistaken for subsequent query string parameter. This means the URL you specified should appear as:
http://localhost:58056/api/links?targetUrl=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnLPE4vhSBx4%26feature%3Dyoutube_gdata
And then inside your Get method, URLDecode the string that is passed as a parameter.
Both methods can be found in System.Web.HttpUtility

How do I bind a URL parameter with a period in it to an MVC action parameter?

A web browser is calling my action with the following URL;
Request URL:http://localhost:4000/MyController/UrlCheck?Menu.Url=sometext
My action is as follows;
public JsonResult UrlCheck(string Url)
{
return Json("Url is " + Url , JsonRequestBehavior.AllowGet);
}
But the Url parameter never gets bound, I've tried the following to no avail;
public JsonResult UrlCheck([Bind(Prefix="Menu")] string Url)
The URL is generated by MVC itself as part of an Ajax post and is tied to a property of a complex object, hence the 'Menu.Url' bit. It won't be easy to change the name of the URL parameter.
Have also tried Menu_Url as a parmeter name. The action is executed so the routing should be working fine.
I haven't come across binding get parameters like that, but I would try binding to a simple viewmodel that is named Menu and has a property called Url.
E.g.
Your Viewmodel
public class SimpleViewModel
{
public string Url { get; set; }
}
Your Action
public JsonResult UrlCheck(SimpleViewModel Menu)
{
return Json("Url is " + Menu.Url, JsonRequestBehavior.AllowGet);
}

Resources