fuvu
fuvu

Reputation: 11

How to configure jackson object-mapper in spring-servlet.xml for @RequestBody conversion?

I want to display a server-side ajax Data-Table. To do this, I need to get the data from my Controller.

My Request would be something like this (from form):

var form = { "order":"12r34e56e", "state":"open"} 

And my Controller should parse it to an Object which I can send to a WebService and receive all open packages from the order in a list. The received list also has to be convert to JSON format to display it in the table.

My header looks like

http://localhost:8080/myWebapp/ordernr/getTableData?{%22orderNummer%22:%2212r34e56e%22}&_=1563526347735

I already tried successfully with RequestParam. But my Form includes some more Filterparams like date1, date2, Adress and so on. So there are too much variables. And I tried in vain to annotate

consumes=MediaType.APPLICATION_JSON_VALUE

I didn´t change my Form-Object-Class "PreparedStatement" because onSubmit my Form without ajax works fine.

in my pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>

in my Controller

@Controller
public class Ordernr extends OrderService {
...
@RequestMapping(value="/ordernr/getTableData", method=RequestMethod.GET)
public String getTable(@RequestBody PreparedStatement ps) {
    OrderService().getOrders(ps);
    return result;
}
...

my ajax call

"ajax": {
    type: "GET",
    url: "${pageContext.request.contextPath}/getTableData",
    contentType:"application/json",
    dataType:"json",
    data: function(data) {
                var form = { "order":"12r34e56e", "state":"open"} 
                return JSON.stringify(form);
          }
    }

My JSON is valid. I tested it in https://jsonformatter.curiousconcept.com/ but i get

"Error parsing HTTP request header. Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986"

I think I need to configure the ObjectMapper from Jackson in my spring-servlet.xml but I dont know how to do it.

Upvotes: 0

Views: 392

Answers (1)

fuvu
fuvu

Reputation: 11

I solved my own question. First I had to add relaxedQueryChars='{}' in my Tomcat server.xml Connector

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" relaxedQueryChars='{}' />

Next thing: Spring does not allow RequestBody for RequestMethod.GET. So I changed to POST and it works. Because of:

<mvc:annotation-driven /> 

I did not have to make any configurations to jackson objectmapper.

Upvotes: 1

Related Questions