0

i have this json POST url'http://localhost:8080/demo/test' from PHP

'{"postby_id":"1","title":"ftkjhg","is_private":"0","status":"1","post_type_id":"1","type":"2","p_id":"1","ve_id":"3","link":"1111111"}'

my webController

    @RequestMapping(value = "/test", method = RequestMethod.POST)

    @ResponseBody
    void creMysqlCall() throws Exception {


        creativityService.creMysqlCall();

    }

Service file

public void creMysqlCall() throws Exception {
        creativityDao.creMysqlCall();
    }

myDAO file

public void creMysqlCall() throws Exception {
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(masterJdbcTemplate).withTableName("posts")
                .usingColumns("postby_id","title","is_private","post_type_id","status","wall_type","p_id","ve_id","link");
        Map<String, Object> creInsertMap = Maps.newHashMap();
        creInsertMap.put("postby_id", "");
        creInsertMap.put("title", "");
        creInsertMap.put("is_private", "");
        creInsertMap.put("post_type_id", "");
        creInsertMap.put("status", "");
        creInsertMap.put("wall_type", "");
        creInsertMap.put("p_id", "");
        creInsertMap.put("ve_id", "");

        creInsertMap.put("link", "");

how can i post data from URL to this DAO am new to java thanks in advance

UPDATE

got this error

HTTP ERROR 405 Problem accessing /backend/test. Reason: Request method 'GET' not supported

New Class file using getter and setter

public class MysqlCall {


    public Object is_private;
    public Object postby_id;
    public Object title;

    public Object getPostby_id() {
        return postby_id;
    }

    public void setPostby_id(Object postby_id) {
        this.postby_id = postby_id;
    }

    public Object getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Object getIs_private() {
        return is_private;
    }

    public void setIs_private(String is_private) {
        this.is_private = is_private;
    }

    public Object getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Object getPost_type_id() {
        return post_type_id;
    }

    public void setPost_type_id(String post_type_id) {
        this.post_type_id = post_type_id;
    }

    public Object getWall_type() {
        return wall_type;
    }

    public void setWall_type(String wall_type) {
        this.wall_type = wall_type;
    }

    public Object getPostto_id() {
        return postto_id;
    }

    public void setPostto_id(String postto_id) {
        this.postto_id = postto_id;
    }

    public Object getVertical_id() {
        return vertical_id;
    }

    public void setVertical_id(String vertical_id) {
        this.vertical_id = vertical_id;
    }

    public Object getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Object status;
    public Object post_type_id;
    public Object wall_type;
    public Object postto_id;
    public Object vertical_id;
    public Object link;

NEw DAO

public void creMysqlCall(MysqlCall call) throws Exception {
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(masterJdbcTemplate).withTableName("posts")
                .usingColumns("postby_id","title","is_private","post_type_id","status","wall_type","postto_id","vertical_id","link");
        Map<String, Object> creInsertMap = Maps.newHashMap();
        creInsertMap.put("postby_id", call.postby_id);
        creInsertMap.put("title", call.title);
        creInsertMap.put("is_private", call.is_private);
        creInsertMap.put("post_type_id", call.post_type_id);
        creInsertMap.put("status", call.status);
        creInsertMap.put("wall_type", call.wall_type);
        creInsertMap.put("postto_id", call.postto_id);
        creInsertMap.put("vertical_id", call.vertical_id);
        creInsertMap.put("link", call.link);

Service file

public void creMysqlCall(MysqlCall call) throws Exception {
    creativityDao.creMysqlCall(call);
}

Controller

// API to test service call @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)

void creMysqlCall(@RequestBody(required = true) MysqlCall call ) throws Exception {

creativityService.creMysqlCall(call);

}

1
  • Use tools like Postman or Advanced Rest Client (both have chrome extensions). You cannot do it from the URL of a browser unless the API received query parameters (which is for GET). Commented Mar 3, 2016 at 15:42

2 Answers 2

0

You will need to create a Value Object class from your JSON with getter and setters.

Then your controller will look like

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
void creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {
   creativityService.creMysqlCall(call);
}

Make sure you have Jackson in your classpath, so in your pom

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

0

This is more of a Spring MVC & Jackson question. You have to create a class like:

public class MysqlCall {
   private String postby_id;
   /* the rest of the fields in JSON */
   /* getters and setters */
}

And then use the @RequestBody annotation to map it:

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object creMysqlCall(@RequestBody(required = true) MysqlCall call) throws Exception {

    //creativityService.creMysqlCall(call);

    return call;
}

In the DAO you just link the object params with the sql mapping.

2 Comments

got this error HTTP ERROR 405 Problem accessing /backend/test. Reason: Request method 'GET' not supported
It would be great if you posted your entire controller and other code. This seems to be a problem with the setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.