After struggling with this issue repeatedly while developing my web apps, I've reached a breaking point and need some help. My frustration might come across as venting, but I'm genuinely stuck.
The challenge I'm facing is sending key-value pairs from my client (vanilla JS) to my backend (Spring Boot Java). Despite trying various approaches, I can't seem to find the right method or combination to achieve my goal. Below is the snippet of my current code that's not working:
Client-Side JS
var object = {
'id' : 1,
'username' : 'jumpthruhoops',
'password' : 'melodysteez'
};
Axios
.post('http://localhost:8080/gameSchedule', JSON.stringify(object))
.then((response) => {
console.log(response.data);
});
Back-End Spring Boot/Java
@CrossOrigin
@RequestMapping(value = "/gameSchedule", headers = "Accept=application/json", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String getSchedule(@RequestBody String user) {
System.out.println(user);
return "test";
}
The output I'm getting is somewhat close to what I expect. It shows a line like...
%7B%22id%22%3A1%2C%22username%22%3A%22tdellard1%22%2C%22password%22%3A%22sisters3%22%7D=
...which seems to be a hex code for the string object I passed in. I'm not sure if this is an issue with Spring Boot or caused by using JSON.stringify. Since the actual objects I plan on passing are more complex, I'd prefer not to deal with decoding the hex code unless there's no other solution.
Given the complexity, I don't want to use @RequestParams("name") String VariableName numerous times in the method parameter. Another method I tried involves @ModelAttribute and (@RequestBody User user), both resulting in errors. The most common error I encounter is:
018-10-30 23:38:29.346 WARN 12688 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
With all these challenges, I need guidance on the best way to send data from Axios (form.serialize, JSON.stringify, JavaScript Object, etc.) and the corresponding method to process this data effectively on my Spring Boot Back-End, so I can work with it as a POJO.