2
votes

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

2
shouldn't your rest call be a POST method? - yogidilip
I tried POST also but the result is same - Rohan Singh Dhaka
Rohan, Would you please post the the client code that actually calls your service? Also, it would really help to see the logs for your request/response pair. - entpnerd
Actually there is no client code. I myself is calling the service from browser after switching on the server. I am typing that json on web browser address. - Rohan Singh Dhaka

2 Answers

1
votes

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"
}
1
votes

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", ...},
});