Mickaël P
Mickaël P

Reputation: 365

Spring MVC : How to populate my Object with JSON

I have an update method (AJAX service) that expect an object. This object is populate by JSon and I want to test it but I can't..

I use spring mvc to set up my service.

My Service :

    @RequestMapping(value="texte/updateMyObject", method= RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public void updateMyObject(MyObject myObject) throws IOException {

        logger.info("UPDATE !!!");

    }

My Object :

public class MyObject {
private Integer id;
private String name;

public MyObject(){}

// getter & setter

}

I use Postman (chrome tool) to test my service : https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm

I think I have to send the good JSON but I don't know which one.

For the moment I have test this pattern :

"MyObject" : {"id" : "1", "name" : "test"}

or {"id" : "1", "name" : "test"}

Edit 1 :

When my service is call, the fields of myObject are all null.

Edit 2 :

I play this kind of HTTP request :

POST /myapp/texte/updateMyObject.sp HTTP/1.1
Host: localhost:8080
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache

{"id" : "1", "name" : "test"}

Edit 3 :

My config spring in my web.xml. If you need my context ask me.

<servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext-springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>*.sp</url-pattern>
</servlet-mapping>

Edit 4 : I made the following test to show you that my Object is not populated.

enter image description here

Upvotes: 1

Views: 2244

Answers (2)

sp00m
sp00m

Reputation: 48817

You need to link a JSON de/serializer library to your project, e.g. Jackson:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>

Then, using the following controller:

@RequestMapping(value = "texte/updateMyObject", method = RequestMethod.POST, consumes = "application/json")
public void updateMyObject(@RequestBody MyObject myObject) {
    // ...
}

When sending a request such as:

POST /texte/updateMyObject
    headers:
        Content-Type: application/json
    data:
        { "id" : "1", "name" : "test" }

myObject shall be filled.

Upvotes: 2

Kico Lobo
Kico Lobo

Reputation: 4404

It's all about the name of the attributes inside the JSON document your controller will receive.

On your action parameters, the type of the parameter must have attributes which will match with the name of the fields on the JSON document it will receive.

For example: take a look at this action:

@RequestMapping(value="texte/updateMyObject", method=RequestMethod.POST)
String doSomething(Person person) {

}

The person class will look like this:

class Person {
   String name;
   int age;

   //getters and setters
}

Your JSON document must be something like this:

{'name':'Henrique', 'age':35}

Upvotes: 0

Related Questions