2

I have to send JSON to the rest end service which is built using spring boot. I am not getting what I am doing wrong. Here is the code

@RestController public class EmailWithTemplate 
{
    @RequestMapping(value="/create-template",method=RequestMethod.GET)
    void CreateTemplate(@RequestBody EmailTemplate emailtemplate)
    {
        System.out.println(emailtemplate.getName());
    } 
}

public class EmailTemplate 
{
    private String name;
    private String body;
    //private String[] values;

    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }

    public void setBody(String body)
    {
        this.body=body;
    }
    public String getBody()
    {
        return body;
    }
}

Plus I have added jackson dependency also in the pom.xml file.

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

After starting the tomcat server when I hit:

localhost:8080/create-template/{"name":"Test", "body":"Test body"}

This is shown in the browser:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jan 29 01:58:00 IST 2016 There was an unexpected error (type=Not Found, status=404). No message available

entpnerd
  • 8,063
  • 5
  • 36
  • 60
Rohan Singh Dhaka
  • 147
  • 2
  • 6
  • 28

2 Answers2

1

You must pass the JSON as POST body, not as URI path.

POST /create-template HTTP/1.0
Content-type: application/json

{"name": "Test", ...}

In jquery for example you'll have to call:

$.ajax({
   type: "POST",
   url: "http://localhost:8080/create-template",
   data: {name: "Test", ...},
});
Zbynek Vyskovsky - kvr000
  • 16,547
  • 2
  • 30
  • 40
  • Sorry I didn't get you. Can you explain a little more. – Rohan Singh Dhaka Jan 28 '16 at 20:35
  • Convert your method to POST. Depending on what client you are using to hit the method you should have option to post a message body and setup a content-type. Please use those options. For postman, follow this url. https://www.getpostman.com/docs/requests – yogidilip Jan 28 '16 at 20:38
1

Basically, your client code is appending the URL localhost:8080/create-template/ with your JSON {"name":"Test", "body":"Test body"}. So, your the URL you are calling is localhost:8080/create-template/{"name":"Test", "body":"Test body"} which the server can't find because the server only knows the localhost:8080/create-template/ resource. That's why you are getting the 404. The JSON needs to be part of the request body, not the request URL.

Also, your server is expecting a HTTP GET request. GET requests can't contain bodies. See: HTTP GET with request body

You will need to do a HTTP POST method with the JSON as the body.

For example, you should send something like the following request:

POST http://localhost:8080/create-template/ HTTP/1.0
Content-Type: application/json

{
    "name":"Test",
    "body":"Test body"
}
Community
  • 1
  • 1
entpnerd
  • 8,063
  • 5
  • 36
  • 60