1

I am trying to pass a dynamic JSON string to my REST Web service which will in turn be used to populate a mysql db. I can do this using a hard-coded JSON string,however I dont know how to pass a dynamic JSON string.

How can I retrieve a parameter passed in via a WebService, parse it out as JSON, and persist that to the database?

Any insight is appreciated as I am very new to this topic. I am attaching my code which I have for the hard-coded JSON string parsing and adding to a mysql db.

My StackService.java

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.google.gson.Gson;
import com.tracker.domain.Flow;
import com.tracker.persistence.HibernateUtil;
@Path("")
public class StackService {

    private Logger LOG = Logger.getLogger(StackService.class);



    /**
     * curl -X GET http://localhost:8080/stack
     */
    @GET
    @Path("")
    @Produces(MediaType.APPLICATION_JSON)
    public void getStock() {

        String stackJsonString = "{\"stack_name\":\"GOOG\",\"id\":null\"}";
        Gson gson = new Gson();
        Stack stock = gson.fromJson(stockJsonString, stock.class);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        String stockName = stock.getstock_name();
        stock.setstock_name(stockName);

        session.beginTransaction();
        session.save(stock);
        session.getTransaction().commit();


    }
}
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
california6586
  • 79
  • 1
  • 3
  • 9
  • 2
    If you want help you should ask a more specific question with more targeted code-examples. For example try to solve the problem yourself as well as you can. When you reach your limit, ask a specific question to help you progress one step forward (including error messages/output you get and code for the problematic part. – Lycha Dec 25 '12 at 22:40

1 Answers1

0

if passing a json paramter is your problem, then u can try using the path parameter.

 @GET
    @Path("{jsonString}")
    @Produces(MediaType.APPLICATION_JSON)
    public void getStock(@PathParam("jsonString") String jsonString) {

        String stackJsonString = jsonString;

but i would recommend you to use post method which would make it simple but you should consider if it is an idempotent operation before u decide to use post.

austin
  • 1,171
  • 1
  • 13
  • 32
  • Thanks a lot for the info. I know how to parse a hard-coded JSON string. However, I dont know how to pass a JSON using a curl command(POST) and use that JSON to populate a db. e.g. crul -X POST -H'Conten-Type:application/json'-d{"stockName":"GOOG"}http://localhost:8080/stock. Currently I am populating the db using a hrd-coded JSON string as shown above in my original question. However,i want to pass the curl command as i just wrote it. If you have any insight then it would be great. Thanks a lot for all your time. – california6586 Dec 26 '12 at 19:28
  • does this help? http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest – austin Dec 27 '12 at 05:02