views:

442

answers:

3

I follow tutorial here on how to create web service using RESTful web service and Jersey and I get kind of stuck. The code is from HelloWorld3 in the tutorial I linked above. Here is the code. I use Netbean6.8 + glassfish v3

RESTGreeting.java create using JAXB. This class represents the HTML message in Java

package com.sun.rest;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;


@XmlRootElement(name = "restgreeting")
public class RESTGreeting {
private String message;
private String name;

/**
 * Creates new instance of Greeting
 */
public RESTGreeting() {
}

/* Create new instance of Greeting
 * with parameters message and name
 */
public RESTGreeting(
    String message,
    String name) {
    this.message = message;
    this.name = name;
}

/** Getter for message
 * return value for message
 *
 */
@XmlElement
public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

/* Getter for name
 * return name
 */
@XmlElement
public String getName() {
    return name;
}

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

HelloGreetingService.java creates a RESTful web service that returns an HTML message

package com.sun.rest;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;


@Path("helloGreeting")
public class HelloGreetingService {
@Context
private UriInfo context;

/** Creates a new instance of HelloGreetingService */
public HelloGreetingService() {
}

/**
 * Retrieves representation of an instance of com.sun.rest.HelloGreetingService
 * @return an instance of java.lang.String
 */
@GET
@Produces("text/html")
public RESTGreeting getHtml(@QueryParam("name")
String name) {
    return new RESTGreeting(
        getGreeting(),
        name);
}

private String getGreeting() {
    return "Hello ";
}

/**
 * PUT method for updating or creating an instance of HelloGreetingService
 * @param content representation for the resource
 * @return an HTTP response with content of the updated or created resource.
 */
@PUT
@Consumes("text/html")
public void putHtml(String content) {
}
}

However when i deploy it on Glassfish, and run it. It generate an exception. I try to debug using netbean 6.8, and figure out that this line return new RESTGreeting(getGreeting(), name); in HelloGreetingService.java cause the exception. But not sure why. Here is the stacktrace

javax.ws.rs.WebApplicationException
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:268)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:637)
+2  A: 
tyvm, that fix it, but if I want to html so that I can display on the web browser, can I convert JAXB into string somehow?
Harry Pham
No, not that I'm aware of. However, Jersey supports page rendering using JSP (http://blogs.sun.com/sandoz/entry/mvcj) but it won't use your JAXB annotations for that.Is it a regular web application you're building or a web service?
+1  A: 
  1. Netbeans 6.9
  2. Create a project Helloworld
  3. Deploy and test the service
  4. Right Click on project properties set relative url as resources/helloworld

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

package helloworld;

import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.xml.bind.JAXBElement; import javax.ws.rs.FormParam; import javax.ws.rs.QueryParam; import java.util.ArrayList; import java.util.List;

/** * REST Web Service * * @author localadmin */

@Path("helloworld") public class HelloWorld { @Context private UriInfo context;

/** Creates a new instance of HelloWorld */
public HelloWorld() {
}

/**
 * Retrieves representation of an instance of helloworld.HelloWorld
 * @return an instance of java.lang.String
 */
@POST

// @Produces("text/html") @Produces("application/xml") // @Consumes("application/x-www-form-urlencoded") // public List getHtml(@QueryParam("name") String user_id) { public List getHtml(@FormParam("test") String user_id) { userData toReturn = new userData(); toReturn.name = user_id; List retUser = new ArrayList(); retUser.add(toReturn); return retUser; }

/**
 * PUT method for updating or creating an instance of HelloWorld
 * @param content representation for the resource
 * @return an HTTP respons with content of the updated or created resource.
 */
@PUT
@Consumes("text/html")
public void putHtml(String content) {
}

}

2nd Class

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

package helloworld;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement public class userData { public String name;

}

And the post html

Reply

Forward

Reply | Harshal Shah

show details 8:11 PM (1 hour ago)

<script type="text/javascript">
  function register(){
//    window.open("/welcome.html",'welcome','width=300,height=200,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
    alert("hey");
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/HelloWorld/resources/helloworld",
        data: "user_id=" + document.getElementById("user_id").value ,
        success: function(msg){
                        window.alert(msg);
                    },
            error: function(xmlHttpRequest, status, err) {
                alert('Status ' + status + ', Error ' + err);
            }
});
    }
</script>

hello

harshal
Using above code you can post any to webservice via php , html(post) if you have question send a mail to [email protected]
harshal