• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending JSON to webservice not working  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following an online example  (http://www.thejavageek.com/2015/12/28/consume-json-in-jax-rs/) to test a POST request by passing a JSON object using Postman client as shown below. :

HelloWorldApplication.java

  
HelloWorldService.java

   
Person.java

   
Error in Postman :





I have tested another HelloWorld example of GET request from here

http://www.thejavageek.com/2015/12/16/jax-rs-hello-world-example-with-wildfly/

and it worked fine. So there isn't any eclipse related issue on my local host/browser.


 
Saloon Keeper
Posts: 1727
235
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the name of your project - JAX-RS_PostExample or JAX-RS_examples?

The example you refer to uses URI: http://localhost:8080/JAX-RS_examples/rest/HelloWorld/writePersonToDB, but in Postman, you are using URI: http://localhost:8080/JAX-RS_PostExample/rest/HelloWorld/writePersonToDB
 
Jack Tauson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:What is the name of your project - JAX-RS_PostExample or JAX-RS_examples?

The example you refer to uses URI: http://localhost:8080/JAX-RS_examples/rest/HelloWorld/writePersonToDB, but in Postman, you are using URI: http://localhost:8080/JAX-RS_PostExample/rest/HelloWorld/writePersonToDB



Yes, I have changed the name of the project in eclipse to JAX-RS_PostExample. Please let me know if there are any other questions I can answer.
 
Ron McLeod
Saloon Keeper
Posts: 1727
235
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confirm that the name of the war file that you are deploying is actually JAX-RS_PostExample.war.
 
Ron McLeod
Saloon Keeper
Posts: 1727
235
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, take a look at your server logs -- they might show you the endpoints.  Here's an example with Tomcat/Tomee:
 
Jack Tauson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Confirm that the name of the war file that you are deploying is actually JAX-RS_PostExample.war.



Great catch. I forgot to deploy the file and it was missing. I deployed the WAR and POSTMAN is still throwing same error. Not sure why this is happening.
 
Ron McLeod
Saloon Keeper
Posts: 1727
235
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:I deployed the WAR and POSTMAN is still throwing same error. Not sure why this is happening.


What do the server logs tell you?

Which server are you using?
 
Jack Tauson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Jack Tauson wrote:I deployed the WAR and POSTMAN is still throwing same error. Not sure why this is happening.


What do the server logs tell you?

Which server are you using?



Thanks. It started working at my end now after fresh deployment. I am using WildFly 10 on my local Eclipse setup. So now, since this is working, I have one design related question:

Since I have to define database connections where I would be calling a stored procedure and passing the JSON related information to the Stored procedure or maybe the object to the stored procedure.

1) Is it a good idea to have getter and setter methods just like it's there in the `Person` class in the example code that I am referring to?

2) In which file I should consider defining database connections from design point of view? I mean is it better to define it in `ExampleApplication.java` Or `HelloWorldService.java` or `Person.java` ?
 
Ron McLeod
Saloon Keeper
Posts: 1727
235
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:Since I have to define database connections where I would be calling a stored procedure and passing the JSON related information to the Stored procedure or maybe the object to the stored procedure.


Rather than storing the JSON representation or a Java object in the database (or passing to a stored procedure), you should just use the fields in the Person class.  For example:
Also, do you really need a stored procedure?


Jack Tauson wrote:Is it a good idea to have getter and setter methods just like it's there in the `Person` class in the example code that I am referring to?


If you want to implement a RESTful-style Web Service API, then you should be thinking of transferring representations of the Persons between the client application and the server, using the various HTTP methods to indicate your intention, and the HTTP status codes to indicate the success/failure of the request.  For example:

POST /api/person
    - create a new Person on the server
    - client provides represenation of Person
    - server provides unique identifier (URI) for newly created Person

GET /api/person/{person-id}
    - get an existing Person from the server
    - server provides represenation of Person

PUT /api/person/{person-id}
    - replace an existing Person on the server
    - client provides represenation of Person

PATCH /api/person/{person-id}
    - update a portion of existing Person on the server
    - client provides partial represenation of Person

DELETE /api/person/{person-id}
    - delete a Person from the server


Jack Tauson wrote:In which file I should consider defining database connections from design point of view? I mean is it better to define it in `ExampleApplication.java` Or `HelloWorldService.java` or `Person.java` ?


It really shouldn't be part of the WS application, resource, or entity.  You should have a Data layer which deals with interfaces with the storage.  For example:

 
Jack Tauson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Jack Tauson wrote:Since I have to define database connections where I would be calling a stored procedure and passing the JSON related information to the Stored procedure or maybe the object to the stored procedure.


Rather than storing the JSON representation or a Java object in the database (or passing to a stored procedure), you should just use the fields in the Person class.  For example:
Also, do you really need a stored procedure?


Jack Tauson wrote:Is it a good idea to have getter and setter methods just like it's there in the `Person` class in the example code that I am referring to?


If you want to implement a RESTful-style Web Service API, then you should be thinking of transferring representations of the Persons between the client application and the server, using the various HTTP methods to indicate your intention, and the HTTP status codes to indicate the success/failure of the request.  For example:

POST /api/person
    - create a new Person on the server
    - client provides represenation of Person
    - server provides unique identifier (URI) for newly created Person

GET /api/person/{person-id}
    - get an existing Person from the server
    - server provides represenation of Person

PUT /api/person/{person-id}
    - replace an existing Person on the server
    - client provides represenation of Person

PATCH /api/person/{person-id}
    - update a portion of existing Person on the server
    - client provides partial represenation of Person

DELETE /api/person/{person-id}
    - delete a Person from the server


Jack Tauson wrote:In which file I should consider defining database connections from design point of view? I mean is it better to define it in `ExampleApplication.java` Or `HelloWorldService.java` or `Person.java` ?


It really shouldn't be part of the WS application, resource, or entity.  You should have a Data layer which deals with interfaces with the storage.  For example:



Thanks for your detailed answer. I have been told to use stored procedure from the database team and hence I am planning on using it. So the basic flow from frond end to the database is as follows:

1) There's a HTML form, user inputs lot of details.
2) I convert all the form fields into a JSON object (either jQuery or PHP).
3) Using PHP, I call this java webservice and pass the JSON object.
4) I intend to pass this JSON object to the stored procedure. The database guy is writing a store procedure to process the this JSON object which can be inserted into the database.

The rationale behind using JSON object is that if in future, we have more input fields in the HTML form, I won't have to manually make lot of changes at lot of places. Considering this approach, if I happen to  proceed with how things are present in my code, and if I define database connection inside the `writePersonToDB` method of `HelloWorlService.java` class, am I doing good in terms of getting an object from the `Person` class. I mean right now, the `System.out.println(person);` prints `12:16:45,829 INFO [stdout] (default task-2) Person [name=prasad, surname=kharkar, address=null]`. So if I have to pass `person` object directly to the stored procedure, does this looks good ?

OR

The whole process sounds redundant to you? By redundant, I mean, I am already accepting the JSON object in the webservice, and then parsing the JSON using `Person.java` class might be redundant? Instead of this, could I just pass the JSON object directly in the stored procedure call which I am planning to define in the `writePersonToDB` method of `HelloWorlService.java` class.

Please let me know if I can answer any questions. Thanks for your time.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!