Java web service

I'm trying to create a web service that will send things like this:

public class Test {
 public static void main(String[] args) throws ClientProtocolException, IOException {
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost("http://localhost/TheServer/webresources/theservice");
  
  JSONObject json = new JSONObject();
  json.put("parameter1", "This is parameter1");
  json.put("parameter2", "This is parameter2");
  StringEntity se = new StringEntity( json.toString());  
  
//  StringEntity input = new StringEntity("{\'parameter1\':\'This is parameter1\',\'parameter2\':\'This is parameter2\'}"); 


  se.setContentType("application/json");
  post.setEntity(se);
  
  HttpResponse response = client.execute(post);
  BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  String line = "";
  System.out.println("Output...");  
  while ((line = rd.readLine()) != null) {
   System.out.println(line);
  }
 }
}

Open in new window



The service code looks like this:
@Path("theservice")
public class ServiceResource {

    @Context
    private UriInfo context;

    public ServiceResource() {
    }
    
    
    @POST
    @Produces("text/plain")
    @Consumes({"application/json"})    
    public String postHandler(String content, String content2 ) {            
        System.out.println("empty postHandler!!!");        
    
        return "testing";
    }
}

Open in new window



What do I need to do to get the json working on the server side? As it is now, it doesn't even like the serverinterface.
It says "Method not allowed" and "The specified HTTP method is not allowed for the requested resource."

What do I need to do to get the json working. I want two objects sent to the serverside and also something returned, lets say a boolean.
mdolandAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
In line 4 in the test class, it should be like this:
  HttpPost post = new HttpPost("http://localhost/TheServer/webresources/theservice/postHandler");

Open in new window

By adding "postHandler". The returned value will be "text" as you using the notation "@Produces("text/plain")".
gurpsbassiCommented:
HttpPost post = new HttpPost("http://localhost/TheServer/webresources/theservice/postHandler");

are you sure?
I don't believe you need to mention the name of the method in a REST URI.


@modland please replace @Consumes({"application/json"})
with @Consumes(MediaType.APPLICATION_JSON) . Not sure why you have got curly braces.
Ahmed MerghaniSoftware EngineerCommented:
If you mean by "name of the method" post, get ...ect, yes you are right. But actually, "postHandler" is the service name in your case !! Try it and feedback us. Also, can you post your "web.xml" here so we help you farther.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

gurpsbassiCommented:
"postHandler" is the service name

The resource is mapped to @Path("theservice")

The @Post method does not have a @Path annotation so it is not mapped explicitly to any path. Thats why you should be calling /theservice and not /theService/postHandler.

Further more, the @Post endpoint seems has two parameters in the method - String content and String content2. This wont work since you are posting with a body consisting of a JSON object. You have set those parameters on a single JSON object in your code
StringEntity se = new StringEntity( json.toString());  


You need to unmarshall this in to a JSON entity (e.g via JAXB annotations on a class)
mdolandAuthor Commented:
gurpsbassi :> After doing some changes according to your specifications it now works.

Just a small thought... I'm going to have 3 Post REST services and wonder if it would be most natural to have them in the same class with each @Post method having a distinct @Path annotation, or is it more natural to have them in different classes? They are related but do different things.
gurpsbassiCommented:
Yes you can have multple post handlers. Just diferentiate them by @Path
If they are related to one another then they should be in same controller.
mdolandAuthor Commented:
... in same controller

And controller means class hre, right?
gurpsbassiCommented:
yes the same resource

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.