Passing a JSONObject as a parameter on Spring MVC
You're confusing GET
and POST
methods.
Either:
- Use
POST
method and actually post the JSON contents to the controller. You can debug this using any REST client, eg. Advanced Rest Client for Chrome. Use
GET
method (as you are currently). But you have to pass the JSON value as an actual parameter calledjsonparam
. So, your example should change to:http://localhost:8080/v1/wsp/url/lookup/jsonparam=%7B%22max_hops%22%3A3%2C%22url%22%3A%22http%3A%2F%2Fbubba.com%2Ffoo%2Fbar%22%7D
The latter is less common.
Edit:
On second look at your URL, I suspect you're confusing two Spring annotations:
@PathVariable("jsonparam")
and:@RequestParam("jsonparam")
You're using @RequestParam
while your URL indicates need for @PathVariable
.
Edit2:
However, as can be read here: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates the path variable can be passed a value of any simple type. I believe JSON isn't one of them, hence your problems.
I would strongly recommend using POST
for interchanging JSON values. However, if that is not an option, I would recommend sticking with GET
method, @RequestParam
for accessing parameter value, and passing the JSON value like in the corrected example above.
Sending Json Data to Spring Controller
You can't use List
as PathVariable
. Make it RequestBody
instead and fix your JS code to send $scope.list
as a body, not append it to url:
-$http.post("/app/orderDetails/saveOrder/"+$scope.list)
+$http.post("/app/orderDetails/saveOrder", $scope.list);
Also create a wrapper object with id
and quantity
fields, something like:
public class CartItem {
private Long id;
private Long quantity;
//getters and setters
}
Then you'll be able to parameterize your List
with this object:
- public @ResponseBody Object saveOrder(@PathVariable List list){
+ public @ResponseBody Object saveOrder(@RequestBody List<CartItem> list){
get pathvariable with sending and recieving json data Rest spring mvc
I found the solution to my problem. There were many mistakes.
(1)The first problem was spring security which was authenticating the url and redirecting it to login page.
I changed this
<http pattern="/myRest/*" security="none" />
to
<http pattern="/myRest/**" security="none" />
(2)Changed myAction1 method to
@RequestMapping(value = "/myAction1/{param1}", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<Abc> myAction1(@PathVariable("param1") String param1, Model model) {
removed an extra slash after {param1}
All that information I got from curl -i parameter
Before solving problem
HTTP/1.1 302 Found
Date: Sat, 07 Feb 2015 05:35:55 GMT
Set-Cookie: JSESSIONID=1kk70yuy7kc501glpcd0m9t4py;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://localhost:8080/login;jsessionid=1kk70yuy7kc501glpcd0m9t4py
Content-Length: 0
Server: Jetty(9.2.3.v20140905)
After Solving problem
HTTP/1.1 200 OK
Date: Sat, 07 Feb 2015 05:37:55 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(9.2.3.v20140905)
[{"age":20,"name":"obama-123"}]
cheers
Spring MVC, pass json object to controler and return view with model attributes set
@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
public String submitReportQuery(@RequestBody ReportQueryMapBean reportQueryMap)throws Exception{
//model.addAttribute("successful", true);
return "queries/SelfServiceQueriesSubmitResults";
}
public class ReportQueryMapBean {
// to do delcare the bean fields to match the request
// and corresponding getter and setter function
}
How to send data from input id in Form to RequestMapping and PathVariable in Spring?
Hii I have tried different methods to send data from input to URL but it is not working then I have tried using javascript if you have no restrictions to use js you can do like this but it will work only if you have one data to send so if you use to model the work will be done quickly or you can try like this.
<form id="insBasicForm" name="insBasicForm" method="post">
<input type="text" id="mobilePhoneNumber" name="mobilePhoneNumber" maxlength="10" placeholder="Telephone Number (Mobile- 10 Digits)" class="w100p" />
<input type="button" name="" value="submit" class="btn" onclick="createUrl()">
<script type="text/javascript">
function createUrl(){
let mobilePhoneNumber = document.getElementById("mobilePhoneNumber").value;
window.location = "/api/v1/checkPhoneNumber/"+mobilePhoneNumber;
}
Related Topics
How to Find Max Duplicate Number Count in Given Array Using for Loop
Time Complexity of Hashmap Methods
Android - Running a Background Task Every 15 Minutes, Even When Application Is Not Running
How to Show Alert in a Jsp from a Servlet and Then Redirect to Another Jsp
Java to Jackson Json Serialization: Money Fields
Http Post Request With Content Type Application/X-Www-Form-Urlencoded Not Working in Spring
Spring Boot Error: Java.Lang.Noclassdeffounderror: Org/Springframework/Util/Assert
How to Find Out the Exact Requested Url in Servletrequest If That Url Contains Path Variables
How to Create Multiple Objects Through Loops in Java
Maven 3.8.0 Compiler - Fatal Error Compiling: Release Version 11 Not Supported
How to Conditionally Enable or Disable Scheduled Jobs in Spring
What Is a Raw Type and Why Shouldn't We Use It
How to Accept Only Numeric Values in a Jtextfield
Spring Boot API Call With Multiple @Requestparam
How to Change Background Colour to Specific Viewholder Items in a Recycleview