Send response from REST to JSP after #POST - java

I have a JSP page (client-side)
<form action="http://localhost:8080/REST-WS/rest/token" method="POST">
<label for="email">Email</label>
<input name="email" />
<br/>
<label for="password">Password</label>
<input name="password" />
<br/>
<input type="submit" value="Submit" />
</form>
It points to a function in REST Web Service (server-side)
#POST
#Produces(MediaType.TEXT_HTML)
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Code verify(#FormParam("email") String email,
#FormParam("password") String password,
#Context HttpServletResponse servletResponse) throws IOException {
Code code = generateRandomCode(email,password);
return token;
}
The problem is I want to give response to the client side containing the random-generated code from the server side.
First, it will be redirected to another JSP page and then the client side can receive the random-generated code from server.
How do I do it?

The problem is that you can't send arbitrary Java objects in a redirect. You can however add the data into query parameters. For example
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(#FormParam("name") String name,
#FormParam("email") String email) {
String message = "Hello " + name + ". Your email is " + email;
URI uri = UriBuilder.fromPath("/index.jsp")
.queryParam("message", message)
.build();
return Response.seeOther(uri).build();
}
Here, you are building a URI from the location of the jsp page, and adding a query parameter to the end of the URI. So the redirect will go to
http://localhost:8080/index.jsp?message=<the message>
From the index.jsp page you can get the parameter with request.getParameter("message"). For example
<h1><%= request.getParameter("message") %></h1>
Another option to work with JSP and Jersey is to implement MVC, which Jersey provides support for. You can check out this answer, though the examples use Maven (to get all the required jars). If you are interested and don't know how to use Maven, let me know and I'll see if I can help you get all the jars you need.
UPDATE
Ajax Example.
Easiest Javascript library to get started with (if you have no experience) is jQuery. I won't really give much explanation about the code, that's kinda out of scope. I would go through some tutorials (W3Schools has some good getting started guides), and there are answers all over SO that can answer your questions.
Here's a complete working html page. Just change var url = "/api/submit"; to whatever endpoint you are sending the request to.
<!DOCTYPE html>
<html>
<head>
<title>Ajax Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function(){
var url = "/api/submit";
$("#submitBtn").click(function(e) {
e.preventDefault();
var formData = $("#nameForm").serialize();
$.ajax({
url: url,
data: formData,
dataType: "json",
type: "POST",
success: function(data) {
var message = data.message;
var date = data.date;
var h1 = $("<h1>").text(message);
var h3 = $("<h3>").text(date);
$("#content").empty()
.append(h1).append(h3);
},
error: function(jqxhr, status, errorMsg) {
alert(status + ": " + errorMsg);
}
});
});
});
</script>
</head>
<body>
<div id="content">
<form id="nameForm">
First Name: <input type="text" name="fname"/><br/>
Last Name : <input type="text" name="lname"/><br/>
<button id="submitBtn">Submit</button>
</form>
</div>
</body>
</html>
Here is the test resource class
#Path("submit")
public class FormResource {
public static class Model {
public String message;
public String date;
}
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Model post(#FormParam("fname") String fname,
#FormParam("lname") String lname) {
String message = "Hello " + fname + " " + lname;
Model model = new Model();
model.message = message;
model.date = new Date().toString();
return model;
}
}
You will need to make sure you have a JSON provider to handle the JSON Pojo serialization or it won't work (the Model won't be able to serizalize to JSON).

Related

Display data on HTML using Thymeleaf

I would like to display the data I get from a search, personalized for my taste. At this moment, It is just plain text.
For example, I am searching for "Titanic", and I get the name, a few links, and some information from IMDB.
I have the following code:
search.html
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="#{/search}" th:object="${search}" method="post">
<p>Message: <input type="text" th:field="*{content}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
result.html
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'content: ' + ${main.content}"></p>
Submit another message
</body>
</html>
SearchController.java
#Controller
public class SearchController {
#GetMapping("/search")
public String greetingForm(Model model) {
model.addAttribute("search", new Main());
model.addAttribute("main", new Main().getContent());
return "search";
}
#PostMapping("/search")
public String greetingSubmit(#ModelAttribute Main main) {
return "result";
}
}
and Main.java
private String content;
private List<Result> finalList;
private List<Result> resultList;
public void setContent(String content) throws IOException {
//code to compute finalList
}
public List<Result> getContent() {
return this.finalList;
}
The main problem is that I have no ideea where to being with. finalList is a list of objects of type "Result", which have fields such as
private List<String> link = new ArrayList<>();
private String name;
private TitleProp titleProp;
and TitleProp has
private String trailer;
private String rating;
private String description;
private String genre;
I would like to manipulate each field to show it on a different way, such as a table with more rows, etc.
Any link or sample of code would help me a lot to understand Thymeleaf and Spring Boot more.
I am coming with an answer to my question. I managed to get the result I wanted using Ajax, as SnakeDoc suggested. It was a long road, mostly because even if I had a working code, I spent a few hours searching for the Forbidden 403 error on ajax post request.
So, for the js part:
function ajaxPost() {
// Here we prepare data for the JSON
var formData = {
moviename: $("#moviename").val()
}
$.ajax({
type: "POST",
contentType: "application/json",
url: "MYURL",
data: JSON.stringify(formData),
dataType: 'json',
success: function (result) {
{
$.each(result,
function (i, title) {
// do whatever you want with what you got from the server
});
console.log("Success: ", result);
}
console.log(result);
},
error: function (e) {
console.log("ERROR: ", e);
}
});
}
If this seems confusing, I access the fields you can see in my question by
title.name, title.link, title.titleProp.description, etc, inside function (i, title)'s body.
For the HTML,
<label for="moviename" style="margin-right:5px">Title:</label>
<input type="text" class="form-control" id="moviename" placeholder="Enter a title"/>
Where moviename is the variable name you get from the input.
Now, on the backend, we have to configure our path
#PostMapping("/MYURL")
public ResponseEntity<Object> addSearch(#RequestBody SearchCriteria searchCriteria)
throws IOException {
// do whatever you want to get a result. I used a custom class "SearchCriteria"
// which has a getter and a setter for the field
// **private String moviename;**
return ResponseEntity.ok(THIS GETS SENT TO AJAX);
}
The main problem was that I have web.security, and you have two choices. First one, you disabling csrf. You have to add this line to your security config.
http.csrf().disable();
in protected void configure(HttpSecurity http) method.
Or, you add csrf to the ajax request. More info on this topic was discussed here
With thymeleaf, you can display a list in html like so:
<tr th:each="student: ${students}">
<td th:text="${student.id}" />
<td th:text="${student.name}" />
</tr>
More info: https://www.baeldung.com/thymeleaf-iteration

why didn't i get the referer parameter in Spring MVC

My related Request handling code as following
#RequestMapping(value = "/customer" , method = RequestMethod.GET)
public String customer(ModelMap modelMap , #RequestParam Integer age) {
modelMap.addAttribute("requestKey", "Hola!");
modelMap.addAttribute("age", age);
return "cust";
}
#RequestMapping(value = "/request" , method = RequestMethod.GET)
public String welcome(ModelMap modelMap , #RequestParam(value = "age" , required = false)Integer age) {
modelMap.addAttribute("requestKey", "Hola!");
modelMap.addAttribute("age", age);
return "request";
}
#RequestMapping(value = "/request" , method = RequestMethod.POST)
public String responseBody(ModelMap modelMap , final #RequestBody Student student){
modelMap.addAttribute("name", student.getName());
modelMap.addAttribute("lastname", student.getLastname());
modelMap.addAttribute("age", student.getAge());
return "request";
}
cust.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Customer</title>
</head>
<body>
<p>${age}</p>
</body>
</html>
request.jsp
<html>
<head>
<title>RequestBody</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
JSONTest = function() {
$.ajax({
url: "customer",
type: "get",
data: {
name: "Okan",
lastname: "Pehlivan",
age:22 },
dataType: "json"
});
};
</script>
</head>
<body>
<p>${requestKey}</p>
<h1>My jQuery JSON Web Page</h1>
<div id="resultDivContainer"></div>
<button type="button" onclick="JSONTest()">JSON</button>
</body>
</html>
I want to get request using ajax, and i have some parameters, (name=Okan e.g). I want to show any patameter on the cust.jsp. When i debug the code for welcome() metod, age assigned the my age. I mapped this value with its key "age". request.jsp file is not changed.
Ajax call will not change the view. It will return the parameters on same page in success : function(response) {} . If you wants to change the view, you need make non ajax GET call on a separate method and pass inline parameters like:
Controller:
#RequestMapping(value = "/customer/{name}/{lastname}/{age}" , method = RequestMethod.GET)
public String customer(#PathVariable(value = "name") String name, #PathVariable(value = "lastname") String lastname, #PathVariable(value = "age") String age) {
//code here
return "cust";
}
JSP
window.open("/customer/Okan/Pehlivan/22 ","_self");
It will return on cust.jsp
The request jsp is not changed because you are not changing it anywhere on the page after the AJAX call.
You do make a request but are not using the response.
Add a success function to your ajax call and then read the data there.
For example :
$.ajax({
url, data , dataType and method as is with an addition of the following :
success : function(responseText){
Set responseText to your <p>
}
});
For showing the age in cust.jsp, the request parameter needs to be passed while calling the url. something like : http://host-path/context-path/customer?age=69
Autoboxing should take care of int to Integer conversion for request parameter, if you see an exception try using int instead.
Some info that might be of help :
When using ${varName} in jsp / javascript, you need the model to
already have that data.
Consider using tags to control the out put of dynamic
variables, they may help.
When making ajax calls to request for data, process the response in a
handler and continue your logic from there.
HTH.

jQuery autocomplete UI with servlet is not returning any data

I have been fiddling with this code fragment from past few hours but I am unable to understand how jQuery's autocomplete UI works. I followed this tutorial http://viralpatel.net/blogs/tutorial-create-autocomplete-feature-with-java-jsp-jquery/
I used same example but instead of sending request to a JSP, I used a servlet. The request reaches the servlet "Fetcher", it executes as well but nothing is returned to the page. Here's the code.
public class Fetcher extends HttpServlet {
[...]
List<String> countryList = new ArrayList<String>();
String param = request.getParameter("term");
countryList.add("USA");
countryList.add("Pakistan");
countryList.add("Britain");
countryList.add("India");
countryList.add("Italy");
countryList.add("Ireland");
countryList.add("Bangladesh");
countryList.add("Brazil");
countryList.add("United Arab Emirates");
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
for(String country : countryList){
out.println(country);
}
[...]
}
Javascript fragment in HTML:
<script>
$(function() {
$( "#tags" ).autocomplete({
source: "Fetcher"
});
});
</script>
HTML form:
<label for="tags">Tags: </label>
<input id="tags" />
The examples on the page seems written for a pro in jquery, http://jqueryui.com/autocomplete/#default . Please, could someone tell how exactly it works, so that I can use it in other places.
The servlet should return the autocomplete data as JSON. There a several options, I have opted for an array which contains an object with label/value properties:
#WebServlet("/autocomplete/*")
public class AutoCompleteServlet extends HttpServlet {
#Override
protected void doPost(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
final List<String> countryList = new ArrayList<String>();
countryList.add("USA");
countryList.add("Pakistan");
countryList.add("Britain");
countryList.add("India");
countryList.add("Italy");
countryList.add("Ireland");
countryList.add("Bangladesh");
countryList.add("Brazil");
countryList.add("United Arab Emirates");
Collections.sort(countryList);
// Map real data into JSON
response.setContentType("application/json");
final String param = request.getParameter("term");
final List<AutoCompleteData> result = new ArrayList<AutoCompleteData>();
for (final String country : countryList) {
if (country.toLowerCase().startsWith(param.toLowerCase())) {
result.add(new AutoCompleteData(country, country));
}
}
response.getWriter().write(new Gson().toJson(result));
}
}
to return the autocomplete data, you could use this helper class:
class AutoCompleteData {
private final String label;
private final String value;
public AutoCompleteData(String _label, String _value) {
super();
this.label = _label;
this.value = _value;
}
public final String getLabel() {
return this.label;
}
public final String getValue() {
return this.value;
}
}
so in the servlet, the real data is mapped into a form suitable for jQuery's autocomplete. I have selected Google GSON to serialize the result as JSON.
Related:
Understanding and Implementing Jquery autocomplete with AJAX source and appendTo
How do you return a JSON object from a Java Servlet
As for the HTML document (implemented in a .jsp), pick the correct libraries, stylesheets and styles:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"> </script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"> </script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="autoComplete.js"> </script>
</head>
<body>
<form>
<div class="ui-widget">
<label for="country">Country: </label>
<input id="country" />
</div>
</form>
</body>
</html>
Related: jQuery autocomplete demo
I have put the Javascript functions in a separate file autoComplete.js:
$(document).ready(function() {
$(function() {
$("#country").autocomplete({
source: function(request, response) {
$.ajax({
url: "/your_webapp_context_here/autocomplete/",
type: "POST",
data: { term: request.term },
dataType: "json",
success: function(data) {
response(data);
}
});
}
});
});
});
The autocomplete function uses an AJAX request to call the servlet. As the result of the servlet is suitable, it can be used as-is for the response.
Related:
What are the “response” and “request” arguments in jQuery UI Autocomplete's “source” callback?
jQuery autocomplete widget
jQuery ajax function

java.io.EOFException Jackson Processor

I am using Spring MVC with the Jackson Processor. When a JSON request is sent to the server as a POST request, the #RequestBody is being deserialized into the object that I need. The problem comes when the GET request is sent, it actually displays Http 500 Internal Server error. The exception that is thrown is:
java.io.EOFException: No content to map to Object due to end of input
org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2022)
org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal (MappingJacksonHttpMessageConverter.java:135)
org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments (HandlerMethodInvoker.java:346)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
No emtpy strings are sent and the correct JSON is sent to the server. I am not sure why this is happening. Below is my code:
JSP - index.jsp
<%#page language="java" contentType="text/html"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#myForm').submit(function() {
var form = $( this ),
url = form.attr('action'),
userId = form.find('input[name="userId"]').val(),
dat = JSON.stringify({ "userId" : userId });
$.ajax({
url : url,
type : "GET",
traditional : true,
contentType : "application/json",
dataType : "json",
data : dat,
success : function (response) {
alert('success ' + response);
},
error : function (response) {
alert('error ' + response);
},
});
return false;
});
});
</script>
</head>
<body>
<h2>Application</h2>
<form id="myForm" action="/application/user/find" method="GET">
<input type="text" name="userId" value="user1">
<input type="submit" value="Submit">
</form>
</body>
</html>
As soon as I change the request type to POST in the JSP and the controller method everything seems to work correctly.
package com.web;
#Controller
#RequestMapping("/user/*")
public class UserController {
#RequestMapping(value = "find", method = RequestMethod.GET, headers = {"content-type=application/json"})
public #ResponseBody UserResponse save(#RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
System.out.println("UserId :" + " " + user.getUserId());
return userResponse;
}
beans.xml
<context:component-scan base-package="com.web"/>
<mvc:annotation-driven/>
<context:annotation-config/>
User.java
public class User implements Serializable {
private String userId;
public User() {
}
// Getters and setters
}
When a GET request is sent the broswer usually displays %%user%%. This is only an example. Does the Jackson processor will still read GET requests?
I don't know where the problem is. I hope you can help.
I read the following description on what the data parameter does in jQuey.ajax() does at http://api.jquery.com/jQuery.ajax/:
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. ....
So it is appended to the URL and there is no request body. That is a behaviour consistent with the GET semantics, if you want to post data use POST.

java.io.EOFException: No content to map to Object due to end of input Spring MVC Jackson Processor

I am using Spring MVC and JSP to send JSON to Spring MVC Controller. Actually, my JSON works for 1 method but does not work for another and I don't understand why. The code is below:
JSP - index.jsp
<%#page language="java" contentType="text/html"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#myForm').submit(function() {
var form = $( this ),
url = form.attr('action'),
userId = form.find('input[name="userId"]').val(),
dat = JSON.stringify({ "userId" : userId });
$.ajax({
url : url,
type : "POST",
traditional : true,
contentType : "application/json",
dataType : "json",
data : dat,
success : function (response) {
alert('success ' + response);
},
error : function (response) {
alert('error ' + response);
},
});
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$('#emailForm').submit(function() {
var form = $( this ),
url = form.attr('action'),
from = form.find('input[name="from"]').val(),
to = form.find('input[name="to"]').val(),
body = form.find('input[name="body"]').val(),
subject = form.find('input[name="subject"]').val(),
fileName = form.find('input[name="fileName"]').val(),
location = form.find('input[name="location"]').val(),
dat = JSON.stringify({ "from" : from,"to" : to,"body" : body,"subject" : subject,"fileName" : fileName,"location" : location });
$.ajax({
url : url,
type : "GET",
traditional : true,
contentType : "application/json",
dataType : "json",
data : dat,
success : function (response) {
alert('success ' + response);
},
error : function (response) {
alert('error ' + response);
},
});
return false;
});
});
</script>
</head>
<body>
<h2>Application</h2>
<form id="myForm" action="/application/history/save" method="POST">
<input type="text" name="userId" value="JUnit">
<input type="submit" value="Submit">
</form>
<form id="emailForm" action="/application/emailService/sendEmail" method="GET">
<input type="text" name="from" value="name#localhost.com">
<input type="text" name="to" value="user#localhost.com">
<input type="text" name="body" value="JUnit E-mail">
<input type="text" name="subject" value="Email">
<input type="text" name="fileName" value="attachment">
<input type="text" name="location" value="location">
<input type="submit" value="Send Email">
</form>
</body>
</html>
The 1st form works correctly and is deserilized in Spring MVC. A sample of that code is:
#Controller
#RequestMapping("/history/*")
public class HistoryController {
#RequestMapping(value = "save", method = RequestMethod.POST, headers = {"content-type=application/json"})
public #ResponseBody UserResponse save(#RequestBody User user) throws Exception {
UserResponse userResponse = new UserResponse();
return userResponse;
}
For the 2nd form I am getting exceptions:
#Controller
#RequestMapping("/emailService/*")
public class EmailController {
#RequestMapping(value = "sendEmail", method = RequestMethod.GET, headers = {"content-type=application/json"})
public void sendEmail(#RequestBody Email email) {
System.out.println("Email Body:" + " " + email.getBody());
System.out.println("Email To: " + " " + email.getTo());
}
}
Below is the stack trace:
java.io.EOFException: No content to map to Object due to end of input
org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2022)
org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have tried using the POST too, but still the same problem. I need to use JSP to send JSON data to the Spring MVC Controller.
I am not sure where the problem is.
beans.xml
<context:component-scan base-package="com.web"/>
<mvc:annotation-driven/>
<context:annotation-config/>
Any ideas?
EDIT
public class Email implements Serializable {
private String from;
private String to;
private String body;
private String subject;
private String fileName;
private String location;
public Email() {
}
// Getters and setters
}
JSON sent is in form2 which is #emailForm.
You should check if you have set "content-length" header.
I ran into the same problem, but I used curl to verify the server side function.
The initial data section in my curl command is
-d "{'id':'123456', 'name':'QWERT'}"
After I changed the command to
-d '{"id":"123456", "name":"QWERT"}'
then it worked.
Hope this gives some hints.
I had this problem when I tried to use POST method with path parameter, but I forgot to put '#PathParam("id") Integer id', I just had 'Integer id ' in parameters.

Categories

Resources