Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Hans Hovan

Ranch Hand
+ Follow
since Mar 03, 2013
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Hans Hovan

We've got a new intern on our team who doesn't have any experience with web applications. Eventually he's going to work towards creating some REST APIs but I'm not sure where a good place to start him would be. Do you guys have any suggestions on a good tutorial to follow where he'll create a simple REST API -- something with basic CRUD operations maybe. The more information and examples the better. Our team is doing a lot of stuff with the Dropwizard framework so incorporating Jersey/Jetty/Jackson type stuff is good. I've done some google searches but there are a ton of tutorials of varying quality so I was wondering if anyone knows of any exceptional ones. Thanks very much for the suggestions and ideas!
9 months ago
Thanks again for the help Ron. I think I might have discovered the issue.

I was collecting all my headers in a map like this:

I tried printing out all of those headers as I added them to the response headers like you suggested.

When I made a POST in Postman, this was the list that was printed out in the debugger (as example):
0 Cache-Control no-cache
1 Accept */*
2 User-Agent PostmanRuntime/3.0.11-hotfix.2
3 Connection keep-alive
4 Postman-Token 804b859e-92a4-4703-a81b-aeb1d7f5d8d4
5 Host localhost:8080
6 id LBM1234
7 Accept-Encoding gzip, deflate
8 Content-Length 7
9 Content-Type application/x-www-form-urlencoded

I then manually created a test map, only adding a few elements at a time and then appending them as response headers to see if that would work. It did at first. I noticed that even though I wasn't adding a 'Content-Length' response header Postman was still displaying one that was ~250 length. I manually added a few more response headers and when I tried to add 'Content-Length' I was able to reproduce the error.

So it seems that Postman has a response header called 'Content-Length' and it was conflicting when I tried to add another response header called 'Content-Length' (that was being generated from the request headers).

So as long as I have something like this:

It seems to work OK and return all of the other headers. (It does also seem to overwrite the 'Content-Type' response header that I add with its own, the response type that I have set in the code json, but it doesn't cause a "could not get response" error).

So I think this solves my problem. Thanks for helping me think through it and for your suggestions Ron.
10 months ago

Ron McLeod wrote:If your headers are in a map, you could use a ResponseBuilder, iterate through the map entries, and build them in to your response header-by-header.

I tried to implement this and it works great for a GET but fails for POST. Any idea why this might be? Both GET and POST are passing in the same values.

When using Postman this code returns the anticipated response if it was from either a GET or a POST (without the response headers being attached)

I then implemented the changes you suggested and came up with this code:

This code appends all the headers and returns the response as expected when a GET request is made. But when a POST is made Postman "could not get any response".
I'll try running this through the debugger and playing around with it some more, but is there something obvious I am missing? Or does RequestBuilder not work with POST requests?

Thanks again for the help.
10 months ago
Thanks for the idea Ron.
I didn't even know a 'ResponseBuilder' existed. That is exactly the type of thing I was looking for. Thanks.
I'll try implementing it and see how it goes. I'll report back if any issues. Thanks again!
10 months ago
Not sure if this is the correct forum, but here goes:

I'm creating a response with code like this:

That all works fine.

Now, what I'd like to do is add response headers. I could do something like this:

But I'll have a variable amount of headers stored in a map. My question then is what would be the best way to add n headers to my response? I have to pretty much work within the confines of the code that I've shown (the map of all the required headers is available to me wherever I need it). I think I'd run the map with a foreach loop appending as I go but I can't find a code example of how to do the actual appending. Not really sure how to approach this. Any help appreciated. Thanks!
10 months ago

Mike. J. Thompson wrote:When a Scanner throws an Exception it does not remove the token that caused the Exception from its input. If nextInt throws an exception then repeated calls to nextInt will also throw an Exception. It won't block waiting for more input because it already has input.

You need to clear the invalid token from the input by calling next() in the catch block.

Ah, I see. Thank you very much with helping me figure that out.
2 years ago
I'm making a small program just to practice various Java exercises. It works fine if I enter the correct variable types. I want to catch when a user enters the wrong kind of input. In this chunk of code though, if the user enters a String when prompted for an int the program just loops continuously and does not give me the option to enter another input even though I am using .nextInt(). Any ideas or can someone point me in the right direction?

The output loops like this:

1: Palindrome
2: Fibonacci
Enter your choice:
Please enter an integer choice.
1: Palindrome
2: Fibonacci
Enter your choice:
Please enter an integer choice.

The loop appears to be working fine but I am not getting a chance to enter another choice. My choice seems to be stuck on whatever I first entered.
2 years ago
I'm learning Java and working on some projects for fun. One issue that I have run in to is that when I use a Scanner object Eclipse warns me that: "Resource Leak: 'scan' is never closed."

So, I added a scan.close(); at the end of my code and that takes care of the warning.

The problem comes in because I have other classes in the same package that also use scanner objects and and Eclipse tells me to close scanner in those classes respectively. However, when I do that it seems like it closes ALL of the scanner objects and I get errors during run time.

Here is an example of what causes the error:

After scanner is closed in the scanTest class and the do loop in test2 is entered again an error occurs at the line test = scan.nextInt();

I tried moving the creation of the scanner object into the do loop just to make a new object every time as well but the error still occurs.

Not sure why this is happening or how I can make sure all my I/O objects are closed out without running into problems.

One post I came across mentioned that when is closed I cannot be re-opened. If this is the case would I just need to make sure a scanner object with is closed at the very end of the program and @suppress all of the other scanner warnings in other classes? Or would that still leave all those scanner objects open (bad)? Or should I make a global scanner variable and then close it at the very end of my program (but some teachers have told me using global variables is frowned upon)?

Any advice would be appreciated. Thanks for helping me learn!
3 years ago
Not sure if I worded that correctly. Hopefully this makes sense:

Say I have super class 'fruit' and a bunch of child classes 'apple', 'orange', etc. If I were to do something like:


How could I check to see if the returned type is a 'fruit' or child of fruit?

So, if I had something like:

Is the code able to tell that apple is a subclass of fruit?

Or is there a better way to do this? Thanks.

4 years ago
Thank you very much for the help.

That gave me some new ways to think about it.

It turned out to get it working the way I wanted it all I had to do was fix my typing error in the second (elements.get(i))), which should have been a j. But your suggestions helped make my code more robust and I have a better understanding of what is going on. Again, Thank you!
4 years ago
I'm writing a program that is supposed to sort objects (people, companies, etc.) based on criteria like last name, then first name, then ID number, and so on. The objects being sorted implement compareTo in their classes. I set up my sort class using the quickSort method (well, I tried to) and can't figure out what is wrong with it. It is strange, because if I add two objects and try to sort them it works OK but if I add three or more something goes wrong...the program will just hang there with no error message (maybe an infinite recursion?) Any suggestions or pointing out of obvious flaws would be appreciated.

For reference, the objects being sorted to have compareTo methods similar to this:

Again, thanks for any help.
4 years ago

This seems to work.

4 years ago
I made a program that allows the user to select a color and then draw a circle on a canvas. The circles are object that get added to an arraylist and are all drawn on the canvas until the user clicks undo (which removes the most recently drawn circle) or erase (which removes all of the circles). Everything regarding that works. However, I also have to have it so that if the user clicks erase and then undo all of the circles in the circle arraylist that were just erased must be redrawn. I can't figure out how to do this and would appreciate any suggestions.

As it is now I've tried to make another temp arraylist and I transfer all of the circle arraylist to it prior to removing all of the elements from the circle arraylist. If the user clicks undo after erase then I transfer the temp arraylist back to the circle arraylist and repaint the canvas.

Here is the code I think is does everything correctly except redraw all of the circles if erase and then undo are pressed:

I changed the button listener to try and get the erase-undo to work:

Not sure what is going wrong but it only redraws some of the circles, or the circles in the wrong order, etc.
Any ideas on a better way to do this? Thank you.

4 years ago
Oh, I figured it out. Needed to get rid of the JTextField and JLabel in the init() method so that I had:

testField = new JTextField("Enter Text Here");
testLabel = new JLabel("???");

Can someone explain to me why that was causing the error? Was it a scope issue where I was making a new object that had no reference to the instance variable and thus the testListener was trying to access a null object?

Thanks again.
4 years ago
Just started learning Applet stuff. For practice I'm just trying to have a TextField and a Label. The user types whatever in the TextField then hits enter and the Label displays what the user entered. The program compiles, the Applet starts up, but when I enter something in the TextField and hit enter I get an error. Probably something simple am missing but I don't see it. Thanks for any help.

The error that displays in the command prompt:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at AppletTest$testListener.actionPerformed( at javax.swing.JTextField.fireActionPerformed( at javax.swing.JTextField.postActionEvent( at javax.swing.JTextField$NotifyAction.actionPerformed( at javax.swing.SwingUtilities.notifyAction( at javax.swing.JComponent.processKeyBinding( at javax.swing.JComponent.processKeyBindings( at javax.swing.JComponent.processKeyEvent( at java.awt.Component.processEvent( at java.awt.Container.processEvent( at java.awt.Component.dispatchEventImpl( at java.awt.Container.dispatchEventImpl( at java.awt.Component.dispatchEvent( at java.awt.KeyboardFocusManager.redispatchEvent( at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent( at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent( at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions( at java.awt.DefaultKeyboardFocusManager.dispatchEvent( at java.awt.Component.dispatchEventImpl( at java.awt.Container.dispatchEventImpl( at java.awt.Component.dispatchEvent( at java.awt.EventQueue.dispatchEventImpl( at java.awt.EventQueue.access$200( at java.awt.EventQueue$ at java.awt.EventQueue$ at Method) at$1.doIntersectionPrivilege( at$1.doIntersectionPrivilege( at java.awt.EventQueue$ at java.awt.EventQueue$ at Method) at$1.doIntersectionPrivilege( at java.awt.EventQueue.dispatchEvent( at java.awt.EventDispatchThread.pumpOneEventForFilters( at java.awt.EventDispatchThread.pumpEventsForFilter( at java.awt.EventDispatchThread.pumpEventsForHierarchy( at java.awt.EventDispatchThread.pumpEvents( at java.awt.EventDispatchThread.pumpEvents( at
4 years ago