NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
netguyhere
Joined: 01 Jul 2010 Posts: 2
|
Posted: Sun Oct 03, 2010 6:38 am Post subject: RESTful Java Client - help needed |
|
|
Hi,
I am trying to learn about RESTful Java client.
I have created a mysql db in my laptop and with netbeans 6.9 I am able to create a RESTful API with which I can get/put data on the web-browser.
Now,I want to use the RESTful Java Client generated by NEtbeans to insert xml data into the database.
The Netbeans generated code for the client (tweetclinet) is:
| Code: | public class tweetclient {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "http://localhost:9050/tweetsaver/resources";
public tweetclient() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = Client.create(config);
webResource = client.resource(BASE_URI).path("tweetss");
}
public ClientResponse post_XML(Object requestEntity) throws UniformInterfaceException {
//webResource.type("application/xml");
webResource.header("Content-Type", "application/xml");
webResource.accept("application/xml");
return webResource.type(javax.ws.rs.core.MediaType.TEXT_XML).post(ClientResponse.class, requestEntity);
}
public ClientResponse post_JSON(Object requestEntity) throws UniformInterfaceException {
return webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(ClientResponse.class, requestEntity);
}
public <T> T get_XML(Class<T> responseType) throws UniformInterfaceException {
return webResource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
public <T> T get_JSON(Class<T> responseType) throws UniformInterfaceException {
return webResource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void close() {
getClient().destroy();
}
/**
* @return the client
*/
public Client getClient() {
return client;
}
/**
* @param client the client to set
*/
public void setClient(Client client) {
this.client = client;
}
}
|
All the converter and entity classes are generated by netbeans:
TweetsConverter.java
------------------------
| Code: |
@XmlRootElement(name = "tweets")
public class TweetsConverter {
private Tweets entity;
private URI uri;
private int expandLevel;
/** Creates a new instance of TweetsConverter */
public TweetsConverter() {
entity = new Tweets();
}
/**
* Creates a new instance of TweetsConverter.
*
* @param entity associated entity
* @param uri associated uri
* @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
*/
public TweetsConverter(Tweets entity, URI uri, int expandLevel, boolean isUriExtendable) {
this.entity = entity;
this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getId() + "/").build() : uri;
this.expandLevel = expandLevel;
}
/**
* Creates a new instance of TweetsConverter.
*
* @param entity associated entity
* @param uri associated uri
* @param expandLevel indicates the number of levels the entity graph should be expanded
*/
public TweetsConverter(Tweets entity, URI uri, int expandLevel) {
this(entity, uri, expandLevel, false);
}
/**
* Getter for user.
*
* @return value for user
*/
@XmlElement
public String getUser() {
return (expandLevel > 0) ? entity.getUser() : null;
}
/**
* Setter for user.
*
* @param value the value to set
*/
public void setUser(String value) {
entity.setUser(value);
}
/**
* Getter for post.
*
* @return value for post
*/
@XmlElement
public String getPost() {
return (expandLevel > 0) ? entity.getPost() : null;
}
/**
* Setter for post.
*
* @param value the value to set
*/
public void setPost(String value) {
entity.setPost(value);
}
/**
* Getter for id.
*
* @return value for id
*/
@XmlElement
public Integer getId() {
return (expandLevel > 0) ? entity.getId() : null;
}
/**
* Setter for id.
*
* @param value the value to set
*/
public void setId(Integer value) {
entity.setId(value);
}
/**
* Getter for ts.
*
* @return value for ts
*/
@XmlElement
public Date getTs() {
return (expandLevel > 0) ? entity.getTs() : null;
}
/**
* Setter for ts.
*
* @param value the value to set
*/
public void setTs(Date value) {
entity.setTs(value);
}
/**
* Returns the URI associated with this converter.
*
* @return the uri
*/
@XmlAttribute
public URI getUri() {
return uri;
}
/**
* Sets the URI for this reference converter.
*
*/
public void setUri(URI uri) {
this.uri = uri;
}
/**
* Returns the Tweets entity.
*
* @return an entity
*/
@XmlTransient
public Tweets getEntity() {
if (entity.getId() == null) {
TweetsConverter converter = UriResolver.getInstance().resolve(TweetsConverter.class, uri);
if (converter != null) {
entity = converter.getEntity();
}
}
return entity;
}
/**
* Returns the resolved Tweets entity.
*
* @return an resolved entity
*/
public Tweets resolveEntity(EntityManager em) {
return entity;
}
}
|
I want to do something like:
| Code: | public class TweetSaver {
public static void main(String[] args) throws ParseException {
tweetclient client = new tweetclient();
Tweets twt = new Tweets();
twt.setUser("JavaClient");
twt.setPost("this post if from Java Client");
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date dt = df.parse("2010-10-02");
twt.setTs(dt);
client.post_XML(twt);
}
} |
When I run this I get the error,
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.tweet.Tweets, and MIME media type, application/xml, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)
at com.sun.jersey.api.client.Client.handle(Client.java:457)
Attaching Tweets class code as well. |
|
| Back to top |
|
 |
gliesian
Joined: 21 Oct 2008 Posts: 50
|
Posted: Sun Apr 14, 2013 1:41 pm Post subject: Answer for this... |
|
|
| Hi, did you ever get an answer for this question? |
|
| Back to top |
|
 |
Edson Richter Posted via mailing list.
|
Posted: Sun Apr 14, 2013 11:23 pm Post subject: RESTful Java Client - help needed |
|
|
There is a question? I've never received it...
Regards,
Edson
Em 14/04/2013 10:41, gliesian escreveu:
| Quote: | Hi, did you ever get an answer for this question?
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|