I am trying to make HTTP POST request to a spring controller with a json array. I am getting a 404 response. I have gone through the following tutorials:
- SpringMVC Ajax validation
- Parsing JSON in Spring MVC using Jackson JSON
- Passing in JSON array to spring MVC Controller
- Passing JSON Array from javascript to spring mvc controller
- JQuery, Spring MVC @RequestBody and JSON - making it work together
- http://spring.io/blog/2012/08/29/integrating-spring-mvc-with-jquery-for-validation-rules/
- http://spring.io/blog/2010/01/25/ajax-simplifications-in-spring-3-0/
But no matter what I try, I always get the same error. I am using Spring v3.0.5 for my project.
"NetworkError: 404 Not Found -
http://127.0.0.1:8080/projectmvc/arttestresults/addsearchqueries"
My jQuery code is given below:
$.ajax({
url: '/projectmvc/arttestresults/addsearchqueries',
type: 'POST'
context: document.body,
dataType: 'json',
data: {'searchQueries': JSON.stringify([{'appName': 'myAppName', 'searchQuery': 'query # 1'}]}
}).done(function(data) {
var dataObject;
try {
dataObject = jQuery.parseJSON(data);
} catch (err) {
dataObject = data;
}
// HTML escape.
// See https://github.com/janl/mustache.js/blob/master/mustache.js#L52
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
var escapeHtml = function(input) {
return String(input).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
};
// Messages
$('#message').text('Success');
}).fail(function(jqXhr, textStatus, errorThrown) {
$('#message').text('Error');
});
My controller code:
@RequestMapping(method=RequestMethod.POST, value="/arttestresults/addsearchquery")
@ResponseStatus(HttpStatus.OK)
public void addSearchQuery(HttpServletRequest request) {
String searchQueryString = request.getParameter("searchQueries");
Gson gson = new Gson();
Type collectionType = new TypeToken<List<ArtSearchQueryRequest>>(){}.getType();
List<ArtSearchQueryRequest> searchQueries =
gson.fromJson(searchQueryString, collectionType);
// Insert the search queries
}
Controller function that renders the JSP
@RequestMapping(method=RequestMethod.GET, value="/arttestresults/showaddsearchqueryform.html")
public String showAddSearchQuery(Model model) {
ArtSearchQueryRequest request = new ArtSearchQueryRequest();
request.setAppName(APPNAME);
model.addAttribute("searchQueryRequest", request);
return "addsearchqueries";
}
JSP page
<!DOCTYPE html>
<html>
. . .
</head>
<body>
<div class="container">
<div id="message" class="units-2 column" style="left:50%; top: 5%; position:fixed">
</div>
<div class="column-group">
<div class="units-2 column">
<h4>Add Search Queries</h4>
</div>
</div>
<!-- Add the form inside this. -->
<div class="column-group">
<div class="units-2 column">
<form modelAttribute="searchQueryRequest" method="POST"
action="${pageContext.request.contextPath}/arttestresults/addsearchquery">
<div class="form-group">
<label for="appNameHidden" class="hide">App Name</label>
<form:hidden path="appName" id="appNameHidden" htmlEscape="true"></form:hidden>
</div>
<div class="form-group">
<label for="searchQuery">Search Query</label>
<input type="text" maxlength="100"name="searchQuery"
placeholder="Enter your search query here.">
<span><a href="#">Add more queries.</a></span>
</div>
<button class="button" type="button">Submit</button>
</form>
</div>
</div>
</div>
<%@ include file="/WEB-INF/views/scripts.jsp" %>
<script type="text/javascript">
$('form button').on('click.submit.queries', function(event) {
event.preventDefault();
$('form input[type=text]').makeSearchCall({
url: '${pageContext.request.contextPath}/arttestresults/addsearchqueries',
type: 'POST',
appName: 'crawler'
})
});
</script>
</body>
</html>
The request never reaches the controller.
UPDATE The log is as follows:
POST http://localhost:8080/projectmvc/arttestresults/addsearchquery 404 Not Found 5ms
NetworkError: 404 Not Found - http://localhost:8080/projectmvc/arttestresults/addsearchquery"
I tried request mapping with request body as shown below
@RequestMapping(method=RequestMethod.POST, value=arttestresults/addsearchquery)
@ResponseStatus(HttpStatus.OK)
public void addSearchQuery(@RequestBody final String requestBodyString) {
String searchQueryString = request.getParameter("searchQueries");
Gson gson = new Gson();
Type collectionType = new TypeToken<List<ArtSearchQueryRequest>>(){}.getType();
List<ArtSearchQueryRequest> searchQueries =
gson.fromJson(searchQueryString, collectionType);
// Insert the search queries
}
UPDATE # 2 Http Fox Post Request returns the following
data: "[{"appName":"crawler","searchQuery":"test1"},{"appName":"crawler","searchQuery":"test2"},{"appName":"crawler","searchQuery":"test3"},{"appName":"crawler","searchQuery":"test4"}]"