Web service parameter is null - java

I'm having a strange problem where data that is send to my web service is null even though the value is there in the http request. Here is the ajax and rest:
$("#updateProfileInfo").click(function(e){
e.preventDefault();
var jobb = $("#jobbEditBox").val();
var bostad = $("#bostadEditBox").val();
var intressen = $("#intresseEditBox").val();
alert(jobb);
alert(bostad);
alert(intressen);
$.ajax({
type: "post",
url: "http://localhost:8080/fakebook/fakebookrest/UserInfoService/json/edituserprofile",
data: {
"jobb" : jobb,
"bostad" : bostad,
"intressen" : intressen
},
success: function(data){
var jsonData = $.parseJSON(data);
alert(jsonData);
}
});
});
#POST
#Path("/json/edituserprofile")
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
public boolean produceJSON(
#QueryParam("jobb") String rawJobText,
#QueryParam("bostad") String rawCityText,
#QueryParam("intressen") String rawIntresseText,
#Context HttpServletRequest request)
{
System.out.println(rawJobText);
User user = (User) request.getSession().getAttribute("user");
System.out.println(""+user.getId());
if(user != null)
return userFacade.editProfileInformation(user.getId(), rawJobText, rawCityText, rawIntresseText);
else
return false;
}
Everthing else is working fine and there are no errors. The callback function is also called. Only problem is that the value of System.out.println(rawJobText); is null.

You need to use #FormParam instead of #QueryParam

Honestly, you may not necessarily use #FormParam. What you can do is to go to your firewall and allow connection to the port that the web service is. By allowing the port the firewall no longer blocks access to data. This would work provided you are sure that your web service is accurate. You can use Postman to test your web service and see the values it is supposed to return.

Related

How To Call Spring MVC Controller From JQuery Ajax Call

i am attempting to call Spring controller method from JQuery ajax call, but its not navigate to corresponding view.
First i am verifying login details by calling authenticateLogin() Spring controller function from ajax call, after successful validate i need to forward the request to corresponding view page, i have tried with below code but its not navigate to another page.
Javascript function:
function authenticatePricingCalcLogin() {
var login = {
userName : $("#username").val(),
password : $("#password").val()
};
$.ajax({type: "POST",
url: CONTEXT_PATH+"authenticateLogin",
data:JSON.stringify(login),
contentType : 'application/json; charset=utf-8',
dataType : 'json',
success: function (response) {
if (response != null) {
if (response.errorMsg != null && response.errorMsg != "") { // Login Error
alert(response.errorMsg);
} else {
// Here i need to call spring controller method and to redirect to another page
// I have tried
$.ajax({type: "GET",
url: CONTEXT_PATH+"navigateMainPage",
data:JSON.stringify(loginDO),
contentType : 'application/json; charset=utf-8',
dataType : 'json'
});
}
}
}
});
}
AuthController.java
#RequestMapping(value = "/authenticateLogin", method = RequestMethod.POST)
public #ResponseBody LoginDO authenticateLogin(#RequestBody Login login){
return authService.authenticateLogin(loginDO);
}
#RequestMapping(value = "/navigateMainPage", method = RequestMethod.GET)
public String navigateMainPage(#ModelAttribute("loginDO") Login login,HttpServletRequest request, Model model) {
try {
// Need to set User Name in session variable
} catch (Exception e) {
}
return "auth/mainPage";
}
Please add / in your path
url: CONTEXT_PATH+"/authenticateLogin",
Hi friend I don't have Comment authority so just answering your question.just comment data part if it is GET Type request and remove it form java side #ModelAttribute("loginDO") Login login, Otherwise just make it POST and check any CSRF token is there or not for safe side.

Spring REST API, Pass a primitive type to the controller

#CrossOrigin(origins = "http://localhost:3000")
#GetMapping("")
public ResponseEntity<List<ToDoItemViewModel>> loadCategoriesByName(#RequestParam(required = false) String name)
{
List<ToDoItemViewModel> allItemsByCategoryName = toDoItemService.getAllItemsByCategoryName(name);
return new ResponseEntity<>(allItemsByCategoryName, HttpStatus.OK);
}
How can i pass just a primitive type to the controller, here is how my $.ajax looks like
$.ajax({
method: 'GET',
url: "http://localhost:8080/todItems",
contentType: 'application/json',
crossDomain: true,
data: 'Work',
success: (resp) => {
console.log(resp)
},
error: (resp) => {
console.log(resp);
}
})
Now when i debug it, it indeed sends the request to this controller, but the String name is always null for some reason, can you just show me what i have to adjust in my ajax request, it's probably something in the data field.
You are using GET Request with request params(#RequestParam annotation).
#RequestParam means that param in request pass across url, like this;
http://localhost:8080/todItems?name=Work
So, you just need to move data to url params.
If you prefer send data across request body, please do not use GET method, use POST instead. Many web servers are not supporting request body in GET Requests

Spring Security CSRF Token not working with AJAX

I have a problem in my spring boot app with the csrf token.
I have a form where I can edit a Person. A Person can have
Let us now imagine that the person has a car and enter this and store it. The next time he wants to delete this car and enter another one. I have created that so that there is a list of all of his cars -- he has the option to remove this from the list. Now I'm starting from these pills and want to send with the corresponding ID to the server a POST. When I try I get a 403 forbidden and I have no idea why.
If I change from POST to GET, then it works.
My JavaScript (taken from this site: http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/#the-csrfmetatags-tag)
var csrfParameter = $("meta[name='_csrf_parameter']").attr("content");
var csrfHeader = $("meta[name='_csrf_header']").attr("content");
var csrfToken = $("meta[name='_csrf']").attr("content");
// using JQuery to send a non-x-www-form-urlencoded request
var headers = {};
headers[csrfHeader] = csrfToken;
$.ajax({
url: "./delete/car",
type: "GET",
headers: headers,
});
$.ajax({
url: "./delete/car",
type: "POST",
headers: headers,
});
My controller methods:
#RequestMapping(value = "/{login}/delete/car", method = RequestMethod.GET)
public ModelAndView delete(#PathVariable("login") final String login) {
System.out.println("Stop");
return new ModelAndView("redirect:" + WebSecurityConfig.URL_PERSONS_OVERVIEW);
}
#RequestMapping(value = "/{login}/delete/car", method = RequestMethod.POST)
public ModelAndView deleteInstEmp(#PathVariable("login") final String login) {
System.out.println("Stop");
return new ModelAndView("redirect:" + WebSecurityConfig.URL_PERSONS_OVERVIEW);
}
Any suggestions?
Thanks in advance.
OK, after strugglin with all that, I get the following result.
I added the fail method to the Ajax construct and get the following message:
"Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '${_csrf.headerName}' is not a valid HTTP header field name."
the official spring site advises that you have to put this: <sec:csrfMetaTags /> or from other sources, this: <meta name="_csrf" th:content="${_csrf.token}"/> in your html file.
After this, you should be able to access these attributes in your JavaScript, but in my case I get undefined and ${_csrf.headerName}.
A last try was to take the value from the hidden value (chapter 24.5: http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/#the-csrfmetatags-tag).
Now, I have the following:
$(function () {
var token = $("input[name='_csrf']").val();
var header = "X-CSRF-TOKEN";
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
});
$.ajax({
url: "./delete/car",
type: "POST",
success:function(response) {
alert(response);
}
});
With this it works like a charm.
Another way, you can use the following code:
$.ajax({
url : './delete/car',
headers: {"X-CSRF-TOKEN": $("input[name='_csrf']").val()},
type : 'POST',
success : function(result) {
alert(result.msgDetail);
}
})
I suggest you first check if a valid csrf token and the header have been generated using chrome debugger. If not, then have you added the <sec:csrfMetaTags /> in the <head>?(you will need to import the spring security taglibs). If using Apache tiles, you will have to add this at the <head> section of the template file being used for the view.
If the token is not empty, then in your security-context/configuration file, check if you have disabled csrf security by any chance. By default it is enabled and needs to be for this process to work.

Spring MVC #ModelAttribute Not Binding Form

I am trying to POST to a Spring MVC controller method via ajax. Using Chrome's developer tools, I can tell that the values from the form are being sent, but in the method, the form values are null.
Here is the jquery call:
var form = $('#renegotiationForm').serialize();
$.ajax({
method:'POST',
url:'/renegotiate/wizard/startRenegotiation',
data:{'renegotiationForm': form},
success: function(data) { this.transitionTo(data); }
});
Here is the Spring MVC method (meant to return only a single string):
#RequestMapping(value="wizard/startRenegotiation", method = RequestMethod.POST)
public #ResponseBody String processStart(#ModelAttribute("renegotiationForm") RenegotiationForm form, BindingResult bindingResult) {
log.debug("Entered showStart(), method=POST");
RenegotiationType type = RenegotiationType.valueOf(form.getRenoType().trim().toUpperCase());
RenegotiationActivity activity = RenegotiationActivity.valueOf(form.getRenoActivity().trim().toUpperCase());
String result = "";
if (type == RenegotiationType.TYPE1 && activity == RenegotiationActivity.ACTIVITY1) {
result = "deleteType1";
}
return result;
}
The values are bound using the Spring Form taglib, and I have confirmed that the path attributes of the form tags match the fields of the RenegotiationForm.
I think it's because you are trying to send an "string" from ajax and you want to get and Object (RenegotiationForm), try to change it to String and Format in Server-side. I recommend you to add the type you are sending from client-side, too.
#RequestMapping(value = "wizard/startRenegotiation", method = RequestMethod.POST, produces="application/json")
Found the answer. Further on in my code, I had a function like this:
var ajaxcall = function() { $.ajax({
// ajax settings
});
}
Unfortunately, setting it up this way doesn't make it work as a jquery deferred, and specifically I couldn't use the .then() function to process of ajax requests.
Thanks for the help.

How can I update my session scoped data model in a front controller java servlet web app page, without reloading the web page?

I have a MVC- structured web application. I have both application-, session-, and request scoped data, And I use a custom made Request Based MVC framework, similar to the ones in Spring and Struts. I have used this question's answer as a tutorial.
I have a java object called ShowModel, which is passed as session scoped data. I use this to keep track of the users selection of visible components on the webpages.
All of the possible visibility selections are represented by a checkbox. They are all set to default of visible/checked when first setting the session data.
I have a listener on all of the checkboxes, that registers change, by class name "toggle", and sends it's id and checked/unchecked status by ajax to the server/servlet. See code example 1. I want to state that my experience with ajax is very limited.
As all of my calls are intercepted by my Front Controller Servlet, I needed to make a corresponding action, to execute the ajax POST-request. This code has been successfully reached, and executed. See code example 2.
My issue, however, is that my action pattern forces redirections. And in some mysterious way, the ajax object responsetext turns out to be the entire html of my index page.
My datamodel is already updated, but as it turns out, this is a faulty approach, due to the front controller strategy pattern.
So does anyone know of a way I can update my session scoped object's variables, without reloading the entire page?
Code example 1
$(document).ready(
function() {
$('.toggle').change(function() {
var id = this.value;
var checked = this.checked;
var json = new Object();
json.id = id;
json.checked = checked;
$.ajax({
url: "selectionmodelupdate",
type: 'POST',
dataType: 'json',
data: JSON.stringify(json),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
$('.event').each(function (index, event) {
//Here I will put code, to update the ".event"'s to be visible or not
});
},
error:function(data,status,er) {
console.log(arguments);
alert("error: "+data+" status: "+status+" er:"+er);
}
});
});
});
});
Code Example 2
public class SelectionModelUpdateAction implements Action {
#Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
ShowModel showModel = (ShowModel) session.getAttribute("showmodel");
AppData appData = AppData.getInstance();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
JsonObject jsonobject = new JsonParser().parse(json).getAsJsonObject();
boolean checked = jsonobject.get("checked").getAsBoolean();
String id = jsonobject.get("id").getAsString();
if(id.equals("A")){
showModel.setASelected(checked);
response.getWriter().write("{isSuccess: true}");
return "index";
}
else if (id.equals("B")){
showModel.setBSelected(checked);
response.getWriter().write("{isSuccess: true}");
return "index";
}
else if (id.equals("C")){
showModel.setCSelected(checked);
response.getWriter().write("{isSuccess: true}");
return "index";
}
response.getWriter().write("{isSuccess: false}");
return "index";
}
}

Categories

Resources