• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read and Send JSON data in a java servlet

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

URL Call: <Protocol:>/contextname/ValidateAccount
The input would be a JSON object as per below format
{"req":[{"name":"abc","clientid":"SAMPLE"}]}
If data is valid then response would be as below
{"res":[{"flg":"ok"}]}

As i am new to JSON , I need to do the above where ValidateAccount is the servlet I need to write to receive the sent data.
I just need to know how to receive and send JSON data.

Can anyone help me to do the above?

Regards,
Deepshan
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on which framework you are using to write your code.

For example in javax.ws.rs all you need to do is put the following annotation before the method you are creating, or if the entire class usual outputs and inputs are Json,in front of the class:
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)

And that's that. Remember to use an appropriate header for both sides when debugging the method (content type as well as accepts json application,look it up), Use JSONObject etc to write and read the output/input.
If you want to debug it manually you can use an extension to your browser which handles post/put commands (I use Postman for Chrome).
By the way, I recommend to use status codes as well to tell the client everything works (so code 200 would be "OK" but you can be more specific, look up response codes).
 
Deepshan Shetty
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but this seems like latin to me
Can I please get a sample code?
 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this seems like latin to you, then it will take a whole course to explain everything you need to know.
Fortunately, someone (a software engineer from California) made this course already,and it is totally free

https://javabrains.io/courses/javaee_jaxrs/


Just to clarify: Developing a server side application is not at all only about the code but about the tools and knowledge you need to know about server-client which are important.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is complete nonsense that an entire course needs to be take to learn how to convert to and from JSON.

Forgetting all those "wonderful" Java frameworks that over-engineer everything and make it tens times harder to do something than without them, just take a look into 3rd-party JSON converters such as Gson or Jackson. I'm sure there are others out there that are just as easy to use.
 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't seem to understand the situation though.
Depending on the framework he is using to implement the servlet, Json conversion is usually done automatically
(there's no need for an extra framework! Especially NOT Gson, which by the way, is not that straightforward to use and needs its own lessons if you want to use ANY non trivial datatype, for example any timestamp).
so converting any object to a JSONObject and back is one line of code (or less on average,since you can specify a class-wide annotation ).
However, that is not the situation,and the actual question is not that narrow.
The situation is that he doesn't know how to implement a servlet at all ("This seem like latin to me"), so some framework needs to be learnt,and for that an entire course is needed 100%.

If he did know how to use whichever framework for implementing servlets,this question would not pop up.
We answer questions to help people,right? Answering questions is the method, it is not what this is all about.
I hope I made this clear for you, if you have any questions do let me know!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's subjective whether Gson is considered hard or easy, so that's irrelevant.

But I don't see any indication that the OP is using a framework at all. If he is, and the JSON conversion is automatic, that's cool. My major beef with your post was implying that he needed to take an entire course before he could hope to tackle this. Needlessly discouraging.

So that leads us back to the OP: are you using a framework? And if so, why haven't you mentioned it?
 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"It's subjective whether Gson is considered hard or easy, so that's irrelevant. "

Compared to one line of code,done automatically, this is absolutely harder than that, and it is very easy to say that,in objective terms.

"But I don't see any indication that the OP is using a framework at all. "

Oh? No framework at all? really? Are you really suggesting that he might be using sockets? Would you advice for that? For a beginner?
I guess that another option is that he implemented his own framework, but guess what I don't think that's very likely.
Kindly notice that my first answer was 'it depends on the framework', to which the answer was "huh?!".

If you answer questions just for the purpose of answering questions and not helping people out, I think you are in the wrong.
If the question is "How to convert objects from json and back in any piece of code"? and nothing else needs to be known,you are absolutely correct. Go Gson!
If the situation is someone is trying to implement a server side application and doesn't know where to begin, then you are absolutely wrong, I hope you can see that. (Unless you advice beginners to use the low level sockets and/or implement their own framework).

Discouraging? As you should know if you have programmed any software, the right way is always the easiest way.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yoni keren wrote:The situation is that he doesn't know how to implement a servlet at all


Actually that is your assumption. He is asking for help with JSON, not servlets, and to me, his reply "seems like latin to me" is in reference to your comments about JAX-RS, not Java EE in general.

yoni keren wrote:... Especially NOT Gson, which by the way, is not that straightforward to use and needs its own lessons if you want to use ANY non trivial datatype, for example any timestamp).


In my opinion Gson is not that difficult to use, and from the example he gave, should be fairly easy to get going.

Simple Gson/Servlet example:
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to put my 2 cents in, if you are going to be writing server side code to handle a REST API, then take a look at Jersey.

The servlet code would end up looking something like:



The annotations handle the conversion of JSON --> Java object and back, and leave you just to deal with the logic.
You would just have to define your 'ValidationRequest' class to match the incoming JSON.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yoni keren wrote:"It's subjective whether Gson is considered hard or easy, so that's irrelevant. "

Compared to one line of code,done automatically, this is absolutely harder than that, and it is very easy to say that,in objective terms.


You are assuming facts not in evidence. There is no evidence that a JSON-capable framework is being used.

Oh? No framework at all? really? Are you really suggesting that he might be using sockets? Would you advice for that? For a beginner?


I'm going to ask you to moderate your tone to be less belligerent and more professional.

Other than the servlet container itself -- which most people would not consider a "framework" -- no framework is need to write a servlet application.

Discouraging?



Yes, the words "then it will take a whole course to explain everything you need to know." are discouraging.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Just to put my 2 cents in, if you are going to be writing server side code to handle a REST API, then take a look at Jersey.


This is a good example of encouraging advice.
 
Deepshan Shetty
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I cannot use REST API here. I just get the JSON objects from a third party. How are they sending it doesn't matter to me .
I just have to retrieve the JSON objects and send an appropriate response back to them. But I'm not getting a proper code to get those JSON objects sent by them.

<Protocol:>/contextname/ValidateAccount


here "ValidateAccount.java" is my page to which they are going to send the below

{"req":[{"name":"abc","clientid":"SAMPLE"}]}



 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are in a Java forum, I will give you the code in Java.
Using org.json (http://mvnrepository.com/artifact/org.json/json/20160212) the code you need will look like that:
(Notice that you should almost never use String literal constants within your code,and I could have made the code shorter, I've made it longer and added String literals so that it will be clearer to you what's going on
 
Deepshan Shetty
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how do I receive the JSON objects sent to me?
Like getParamter or some HTTPConnection call or so??

{"req":[{"name":"abc","clientid":"SAMPLE"}]}



This how do I read it?
 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...Do you know how to read a simple text which is sent to you as a response? (forget Json formatting for now, just read the response as a String).
On top of that I have to ask if you are indeed using Java as your programming language.
 
Deepshan Shetty
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I'm using java and we normally use getInputStream,get parameter etc
 
yoni keren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,So I don't understand the question? :P What is stopping you from reading the response as a String, which is something you know how to do and then converting it to a JSONObject, which is the code that I have written? IE what is the question? :P

The only thing you should worry about is put the (key,value) (Accept,application/json) in your header to make sure that they will send you a Json-formatted text (if they support that, which you said that they do).
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say above:
"How are they sending it doesn't matter to me ."

It clearly does matter if you don't know how to read this information.

Where is your code sitting?
You say you use getInputStream and getParameter, so this is a servlet?
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
Simple Gson/Servlet example:


Wandering off-topic, but what is the point of using GsonBuilder? I don't use Gson a lot, but I use it like this:

What's the difference? Am I missing some important concept?
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:.. what is the point of using GsonBuilder?


You are right - if no customization is required, new Gson() would be better. Thanks for pointing that out.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should read up on Jackson or other marshalling frameworks if you want to do a proper job.
Reading it as a String will only result in bad and unmaintainable code.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you expand on this? Most of the examples I see just send JSON as a String. Why is this a problem and how does Jackson do it?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic