Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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

I figured out my need:


and then

to get the String value I was looking for.
3 months ago
I have a Spring Boot application and I am adding a Redis component to it. I'm fairly new to Spring Boot and very new to Redis. What I need to do is get a JSON String from a Redis server and then the application will do some manipulations/handle that String. Basically I just need the Read of a CRUD setup. A different application manages the contents of the Key/Value, this application just needs to make sure it is using the most updated Value from time to time before it does stuff.

All of the tutorials I'm finding suggest to make a Redis Configuration and Repository. Something like this:

The tutorials I've been looking at (like this one: suggest that to get the value of a particular key you'd do something like this:

But I don't have a DataModel I'm directly trying to turn my find call into. I just want a String value associated with a particular Key. So, I'm thinking that I need to change in the Configuration and Repository to because I'm using a String to get a String?

I'm also not really using hash functionality like in the examples where there are different ids as part of the Key. I just need a way to get/find a Key's value, something more like a JedisPool? Along the pseudo-code of:

But I'm not sure how to set that up within what I'm already working with. Or maybe my overall approach is way off? Appreciate any help! Thanks.
3 months ago

Junilu Lacar wrote:

Hans Hovan wrote:
If the ProcessorController class contained something like this:
So part of the ProcessorController class would look like this instead:

I think it would make sense since we've linked the interface with its implementation. But it isn't that way. So what is going on here? Any help is appreciated!

If you do that, you've negated the usefulness of programming to an interface instead of an implementation. This design would be far inferior to the other since it's programmed to a specific implementation. What you're seeing is Spring's @Autowire magic in action where it will find a class marked with @Component and inject it into something marked as @Autowired that takes an IProcessor implementation. It can be any IProcessor implementation and not a particular one. This creates a "seam" in your program where you can switch out the implementation with a different one, say for testing purposes, and the program should still work as expected.

Thanks much for the explanation. Really appreciate it. Follow-up question: What if there were two classes that implemented IProcessor though? How would Spring know which one to use?

Edit: It looks like this was already answered in Rob's post. Makes sense. Thanks.
4 months ago
I'm working on a large application that uses Spring Boot. In it, an interface is used in a way that I don't understand.
I've stripped down the code to the only parts that I think are relevant to the question. There are four files that I have put in order of application flow.

A controller object is created.
It is told to process some stuff.
The controller tells an interface processor to do the work.
There is a processor that implements the interface processor.

It was my understanding that when using an interface you'd do something like:
In other words, assigning the interface an implementation. But in this sample program it seems the processor implementation is implicitly assigned, not in the code as far as I can tell. I'm trying to figure out if this is some Spring wizardry or if I'm just understanding interfaces wrong:

If the ProcessorController class contained something like this:

I think it would make sense since we've linked the interface with its implementation. But it isn't that way. So what is going on here? Any help is appreciated!
4 months ago
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!
3 years 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.
3 years 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.
3 years 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!
3 years 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!
3 years 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.
5 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.
5 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!
6 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.

6 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!
6 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.
6 years ago