java - How can I use put method to insert data to my MySQL databse using restful webservice -
i use this tutorrial , created table (id ,username) prints; , want insert data using restful web service
what should put in content text box ?
this paints class
/* * change template, choose tools | templates * , open template in editor. */ package entities; import java.io.serializable; import javax.persistence.basic; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.namedqueries; import javax.persistence.namedquery; import javax.persistence.table; import javax.validation.constraints.notnull; import javax.validation.constraints.size; import javax.xml.bind.annotation.xmlrootelement; /** * * @author subhi */ @entity @table(name = "prints")
@xmlrootelement(name = "prints") @namedqueries({ @namedquery(name = "prints.findall", query = "select p prints p"), @namedquery(name = "prints.findbyid", query = "select p prints p p.id = :id"), @namedquery(name = "prints.findbyusername", query = "select p prints p p.username = :username")}) public class prints implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @column(name = "id") private integer id; @basic(optional = false) @notnull @size(min = 1, max = 30) @column(name = "username") private string username;
public prints() { } public prints(integer id) { this.id = id; } public prints(integer id, string username) { this.id = id; this.username = username; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } @override public int hashcode() { int hash = 0; hash += (id != null ? id.hashcode() : 0); return hash; } @override public boolean equals(object object) { // todo: warning - method won't work in case id fields not set if (!(object instanceof prints)) { return false; } prints other = (prints) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @override public string tostring() { return "entities.prints[ id=" + id + " ]"; } }
i tried use put method got error
and why didn't have post method in combobox?
controller code java ee 6
/* * change template, choose tools | templates * , open template in editor. */ package service; import entities.prints; import java.util.list; import javax.ejb.stateless; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.ws.rs.consumes; import javax.ws.rs.delete; import javax.ws.rs.get; import javax.ws.rs.post; import javax.ws.rs.put; import javax.ws.rs.path; import javax.ws.rs.pathparam; import javax.ws.rs.produces; import org.netbeans.saas.restresponse; import org.netbeans.saas.google.googlemapservice; /** * * @author subhi */ @stateless @path("entities.prints") public class printsfacaderest extends abstractfacade<prints> { @persistencecontext(unitname = "test3pu") private entitymanager em; public printsfacaderest() { super(prints.class); } @post @override @consumes({"application/xml", "application/json"}) public void create(prints entity) { super.create(entity); } @put @override @consumes({"application/xml", "application/json"}) public void edit(prints entity) { super.edit(entity); } @delete @path("{id}") public void remove(@pathparam("id") integer id) { super.remove(super.find(id)); } @get @path("{id}") @produces({"application/xml", "application/json"}) public prints find(@pathparam("id") integer id) { return super.find(id); } @get @override @produces({"application/xml", "application/json"}) public list<prints> findall() { return super.findall(); } @get @path("{from}/{to}") @produces({"application/xml", "application/json"}) public list<prints> findrange(@pathparam("from") integer from, @pathparam("to") integer to) { return super.findrange(new int[]{from, to}); } @get @path("count") @produces("text/plain") public string countrest() { return string.valueof(super.count()); } @override protected entitymanager getentitymanager() { return em; } @get @produces("text/html") public string getgooglemap() { // drag , drop getgooglemap operation here try { string address = "16 network circle, menlo park"; java.lang.integer zoom = 15; string iframe = "false"; restresponse result = googlemapservice.getgooglemap(address, zoom, iframe); return result.getdataasstring(); } catch (exception ex) { ex.printstacktrace(); } return ""; } }
put request body part there.if going update entity should pass dto data should updated.i didn't go through link.this part can in json or xml.(like below)
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <prints> <id>1</id> <username>test</username> </prints>
you have annotate dto lets printsdto below
@xmlrootelement(name = "prints") public class printsdto { private int id; private string username; // have getters , setteres above }
Comments
Post a Comment