java - How to extend / edit Netbeans' auto-generated REST services? -


i new automatic generation of rest services code database in netbeans 8.

disclaimer: after discussion (in comments), realized should warn avoid question if not familiar automatic generation of rest service db in netbeans (https://netbeans.org/kb/docs/websvc/rest.html). that's because need know going on , put hands in order edit them. don't provide non-working code here, rather want know should in order edit such services. provide example of obtain.

i did automatic code generation of rest services db , obtained entity classes , service "facade" classes. need extend / edit services , don't know put hands.

for instance, consider following scenario. have student , he/she passed many exams. db perspective, student-exam 1 many relationship. when test rest api , perform id of student, resulting json not contain collection of exams associated student, expected. , how should change auto-generated service code in order obtain exams collection within student's json?

in other words, assuming perform ../student/12, want obtain is:

{    "id":12, "name":"marco", "age":26, "exams": [    { "id":1, "exam_name":"computer networks" },    { "id":15, "exam_name":"algorithms"}   ] } 

best regards

so seems got working, don't know if elegant way it.

here's did.

1) go auto-generated child entity class (i.e. exam.java) , add custom namedquery namedqueries annotation. instance,

@namedqueries({ ... default auto-generated stuff ... @namedquery(name = "getexamsofstudent", query="select e exams e e.student.id =: studentid")}) 

2) go auto-generated child rest service class (i.e. examfacaderest.java , add method retrieve children of parent, using namedquery defined in entity class. instance,

@get @path("studentid/{studentid}") @produces({"application/xml","application/json"}) public list<exam> getexamsofstudent(@pathparam("studentid") integer studentid) {     javax.persistence.query query = getentitymanager().createnamedquery("exam.getexamsofstudent");   query.setparameter("studentid", studentid);   return query.getresultlist(); } 

at point have rest service retrieves children of parent entity.

however, not asked for. retrieve exams collection, inside student json, when performing (by id) of student. in order go father entity rest service, (i.e. studentfacaderest.java) , edit find method.

does know more elegant way it?

edit 1: i'm trying second method (to retrieve exams collection, inside student json, when performing of student) , see collection not serialized output json. advice on this?

edit 2: got it. "generally extending" service first part of answer above good. however, if want obtain serialization of collections mapped relations in db, remove relative @xmltransient annotations in entity auto-generated classes. take care avoid circular references, need add @xmltransient annotation "getparent()" method in child entity class. example: (a) go student entity class , remove @xmltransient annotation getexamscollection() method; (b) go exam entity class , add @xmltransient annotation getstudent() method.

hope helps.

best regards


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -