Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!

Simon Ritchie

Ranch Hand
+ Follow
since Jul 16, 2015
Simon likes ...
Hibernate Eclipse IDE Spring
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 Simon Ritchie

Hi all,

I’ve written a method to get the number of n-grams of a specified size n from a String.  For example, the number of 2-grams in the sentence “How now brown cow” is 3:

How now
now brown
Brown cow

Here is my method:

My concern is that the nested for loop here might adversely affect performance and that there would be a faster, more memory-efficient way to perform this operation.  Are there any suggestions?  Or are there any common libraries out there that perform this function that I've overlooked?
5 months ago
Hi all,

I’m developing for a microservice application.  There are several different microservices but all of them use the same database.  I’ve been presented with a user story that requires querying data saved by one microservice (A) and enriching it from another microservice (B).  As I understand it, in a true microservices architecture this should be done by making an API call from B to A using something like a RestTemplate or HttpClient.  But seeing as the tables that contain the information I need are accessible to B from the database, is it a better design decision to simply write a DAO that gets the data I need?
1 year ago

Campbell Ritchie wrote:

Simon Ritchie wrote:. . . Can this be accomplished using streams?

Yes, it probably can, but why? Don't go trying to specify an implementation detail at such an early stage in the process.

Thanks Campbell, I hadn't seen that page before.  I'm trying to use streams and functional programming as much as possible in my development as I'm trying to get up to speed with the technology and using if statements where I work seems to be looked down on.  It's not that they're forbidden but often when code is reviewed I'm encouraged to find a more functional programming solution to conditionality.
1 year ago

I'm looking for a way to rewrite the following code using Java 8's functional programming capabilities.

I have a method that returns a list of objects from a database table.  If the list is empty then I return a list of objects from a second, different database table.  Currently this is how I'm handling this logic:

Can this be accomplished using streams?
1 year ago
Yes, sorry, I'm using Spring Boot 2.
1 year ago

Stephan van Hulst wrote:Well, since you can't return a List<String> without using some sort of format, how is it converted?

Sorry Stephan, not sure I understand.  I'm using Postman to validate the responses from the endpoint and this is the raw data that's being returned:

["item 1","item 2","item 3","item 4"]

I'm not doing any conversion of the data that I can see.  The repository returns a List of Strings, as does the service and finally the controller.
1 year ago
It's not JSON, it's just a List<String> variable that's being returned (one of the front-end developers here requested it for an autocomplete text field).  I have additional test cases where I'm using the MockMvcResultMatchers content() method to check the JSON response against an expected response but those cases relate to methods that return JSON data.

1 year ago

Junilu Lacar wrote:What code are you testing there though? Certainly not the mock object. Are you testing the way you are parsing the response?

Sorry, I should have been more clear.  Yes, I'm testing the response.  The code I've posted there is taken from an integration test I've written for the microservice.  I'm using a MockMvc object and I've also configured the class so that a couple of SQL scripts will run before each test.  The scripts will create the database tables and populate them with mock data that I intend to use to verify the service is returning data correctly.
1 year ago
If I can bump this thread again, I've a short question on the best way to test an endpoint response.  

I have an endpoint that returns a List of Strings from a database table

I've mocked my data so that this method returns a List with the following items only

"item 1", "item 2", "item 3", "item 4"

Currently the endpoint returns the List of Strings in the following format

[""item 2"",""item 3"",""item 4"",""item 1""]

In my integration test class, I'm using the following code to test this

The test is successfully passing but I don't like the way I'm replacing characters from the result String, splitting the String by commas and storing the output in a List.  Is there a better way to do this test?
1 year ago
Hi all,

From reading the official documentation on the CrudRepository interface, I see that the findById method returns an Optional.  I understand why - there might be no database record for the id being searched so it makes sense to use the Optional class to avoid NullPointerExceptions.  Almost all of the tutorials on the Spring Boot Data JPA package show you how to write custom queries, like here for example.  But none of these tutorials feature an example where a custom method returns an Optional.  If I'm writing a Repository and I'm writing custom queries, shouldn't they also return Optionals seeing as the principle set down by findById would indicate that this is best practice?

EDIT: I just came across this blog post that outlines good reasons for not returning Optionals in a Repository but I'd be interested to read what the folks on here think.
1 year ago

I need to write a RESTful service that will retrieve data from a database and present the information as a paginated list of database objects, i.e.:
I’d like to implement this as a reactive web service and from the tutorials available online I see that reactive services make return Flux<> objects.  Is it possible to convert a Flux<> list into a Page<>?
1 year ago
Unfortunately not as there is filtering taking place (a requirement) before the antmatcher configuration stuff is engaged.
1 year ago

I've recently developed a custom Spring Security filter that extends the GenericFilterBean class, of the sort described in this example.  The filter will control access to a web application (specifically, it will block certain HTTP requests to certain URLs).  I want to write a good series of integration tests for this filter but as it's the first one I've written I'm not sure what the best practices are.  My instinct is to write three tests

  • A test that performs a HTTP request to a URL that I want to be accepted correctly (expected response HTTP 200)
  • A test that performs a HTTP request to a URL that should be blocked (expected response HTTP 403)
  • A test that ensures calls to an existing API that is based on one of the screened URLs still works fine (responds with data)

  • Have I covered all bases here and are there any good resources on Spring integration testing that you can recommend?
    1 year ago
    Yeah, I found out that the reason it was interpreting my POST as a GET was because the request wasn't coming in with a "/" appended.  You were right about the down casting, thanks.
    1 year ago
    Resurrecting this old problem as it's still something I've yet to resolve.

    I hope people here don't mind me posting something from another forum but the solution described here is pretty much what I'm looking to be able to do.  The problem is that Java's ServletRequest class doesn't appear to have a method that tells you what kind of HTTP request has just been made (GET, POST, etc).  I've tried downcasting the ServletRequest object to a HttpServletRequest and then I've used Postman to send a POST request to the URL that I'm filtering on but the HttpServletRequest seems to see the incoming method as a GET instead of a POST.  Is there any way to determine what HTTP request has been made on a ServletRequest without downcasting?  None of the available ServletRequest methods seem to have this information.
    1 year ago