I already asked how to reload a specific part of a JSP page with Spring MVC on here.
I needed to pass a value with AJAX to my Controller and that didn't really work. But I could solve it without JSON.
What do I want?
I want to be able to pass this exact value as a JSON string because I'll need to get JSON working for further functions anyway plus I'm curious about why I it doesn't work for me.
At the moment I'm getting the Error: 415 (Unsupported Media Type).
My Code:
MainController.java
@RestController
public class MainController {
// Delivers the refresh-information for the ajax request, when the detail view gets reloaded
@RequestMapping(value = "/refreshDetail", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_HTML_VALUE)
private ModelAndView refreshDetailView(@RequestBody IdObject id, ModelMap model){
DetailViewBean dvb = Dao.getDetailViewData(id.getId());
model.addAttribute("detailSearchResultPerson", SupportMethods.getDetailViewDataRecordsPerson(dvb));
model.addAttribute("detailSearchResultCompany", SupportMethods.getDetailViewDataRecordsCompany(dvb));
/*
getDetailViewDataRecords: Array of single Data Records (Label & Data)
getDetailViewData: Bean with fetched Data from the DB
*/
return new ModelAndView("detailView", model);
}
}
main.js (ajax request)
$.ajax({
type: "POST",
accept:"text/html",
contentType: "json/application;charset=UTF-8",
dataType: "html",
url: "/refreshDetail",
data: JSON.stringify({"id": id}),
success: function(response) {
$(".containerDetailView").html(response);
}
});
IdObject.java
public class IdObject {
int id;
public IdObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
WebInit.java
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
...
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.6</version>
</dependency>
What have I tried?
I tried to configure WebInit.java like that:
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(WebConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(RootConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
But that didn't change anything.
Then I also tried to configure the Spring Message Converters myself, but that didn't help as well.
I don't feel like there's anything wrong in the AJAX request, or how I handle it in the Controller but please point it out if there is.
If anyone could give me any advise on how to solve this problem, it would be highly appreciated (: