I have a list of subjects that can be registered to students. A subject has a property maxParticipants.
Example:
Subject: Spanish
Max Participants: 5
I want to update the available subject places by a Spring Flux with a given interval. This is no problem (every 10s i do a select count on the datasource to get the available places).
My question is:
How do i update the frontend being rendered with thymeleaf?
There are numerous examples using thymeleaf and spring webflux but all of them are dealing with a large list where the #Controller Model is set with a certain thymeleaf type.
I just want to update existing records.
Do i need to do this with plain javascript/jquery?
Thanks for your tips!
Thomas
You should use EventSource in the web page. Follow example
https://www.mkyong.com/spring-boot/spring-boot-webflux-server-sent-events-example/
Related
Disclaimer: I couldn't come up with a better name for the question - but I'll try to explain what I'm trying to achieve.
Let's assume we have a client-server application with client-side internationalisation. At this moment it is out of the question to move all the internationalisation to the backend.
The backend service saves some tags in the database as strings. (e.g.: CAR)
The client of this service performs a GET, translates the tags and displays them in the view (e.g.: Car (en), Auto (es)).
Now we want to add some filters to a query based on the tags.
Everything works fine as long as we have a list of pre-defined tags that are mapped to the tags stored in database, but how would you implement a solution where a user can type in a text filter (in any language) and the backend to return the values matching with the "original" tag?
i.e.: type in auto, backend returns CAR.
So the question: i have the following Json:
{
"type": "ipv4",
"value": "1.2.3.4",
"firstSeen": "2020-07-10 15:00:00.000",
"totalCount": 8
}
i need to create a spring boot microservice from it ,with the following restrictions:
TotalCount cannot be less than 0 and cannot be more than 100.
firstSeen date should ALWAYS be converted to ISO 8601 format. The user can enter the date
in any string format. Return error if it is not well formed.
Expose the following RESTful APIs
Create a new record (as shown above, id auto-generated)
Get record by value
as this is my first time working with microservice,i can not understand this problem,is there anyone can help me with this please?
You will need to create a basic Spring Boot project using Spring Initializer, if you are using Intellij you can use this link as reference https://www.jetbrains.com/help/idea/your-first-spring-application.html#create-new-spring-boot-project.
Then add a new controller method which accepts a Json Request. Since you are trying to create a new record, I suggest you use POST method. Json Request will accept the 4 input parameters you mentioned. This is very basic and you should be able to find it in pretty much any Spring boot tutorial online. you can refer for example https://dzone.com/articles/simple-spring-boot-post
This Json Request can have validator annotations which check for the criteria you give. For example you can have #Size(min=0, max=100, message="TotalCount cannot be less than 0 and cannot be more than 100"). https://www.baeldung.com/jpa-size-length-column-differences
For date you might need to write a custom validator to check the specific format you want. For creating a record I guess you mean adding it to database. here you can configure your database using the yaml file, again there are lot of online resources on how to configure a Database in your spring boot project. https://blog.tericcabrel.com/write-custom-validator-for-body-request-in-spring-boot/
Since its your first time, it might take a while to figure out various details but I assure you once you get a hold of it, its going to be easy.
Is there a way in Spring Social Twitter to search for tweets but exclude retweets? Currently I use the search method like this but I finds - of course - also retweeted tweets.
twitter.searchOperations().search('stackoverflow', 100)
May be little late to answer, but its actually now possible to filter out the retweets from search results.
"puppy -filter:retweets" containing “puppy”, filtering out retweets
So, in above case with Spring Social, you can just change the query to
twitter.searchOperations().search('stackoverflow -filter:retweets', 100)
This would return all tweets containing stackoverflow, and filter out retweets.
You can check all search operators for standard(Free) API.
Regarding to the Twitters API 1.1 it's currently not possible to exclude retweets when searching for tweets. Nevertheless I have overlook Spring model class org.springframework.social.twitter.api.Tweet for a tweet. The is a property retweet on that class that can be use to filter the result.
I'm developing an adminstration interface for a set of tables. I need to implement functionalities such as listing, sorting, filtering and pagination.
I'm using Spring Boot as a starter and Spring Data Jpa for repository. I've searched the Web for some examples about a complete solution that includes all the above functionalities. What I found included almost all of them, but appearently if there was pagination and sorting there wasn't filtering or viceversa.
Now I'm storing the filter in a application object on session using #ModelAttribute but I know that is now a good design because the applciation will extend and it becomes hard to maintain. I'm also using Page and Pagination for pagination purpose and using Specifications for filtering.
What I want is to submit all the data, i.e.: search fields, sorted fields and current page, in a single request. Off course if the search fields are not empty the pagination will be reinitialize.
Another thing is that I don't want to use jQuery datatables but plain HTML and form submission.
Here are some tutorials and examples that I found:
Link 1
Link 2
Thanks in advance
EDIT html form included
Here is the structure of my table and my pagination section:
<form method=post action=someLink>
<table> -populated from controller using Thymeleaf - </table>
<div class=pagination>
<ul> - actually this div is build using the page object returned from server -
<li><a href=link/?page=;size=;>1</a></li>
<li><a href=link/?page=;size=;>2</a></li>
<li><a href=link/?page=;size=;>3</a></li>
</ul>
</div>
</form>
As you can see the form is separated from my pagination div. When I click on a page number a get request is send to the server and executes the query with the stored filter. When I submit the form the page number is not taken into consideration because the number of pages can change.
So my question is how to build the form to include the pagination in one single submit.
I'm thinking instead of using a's to use input elements so on the server I can read the data from them. I don't know how to submit that post request with the pageable attributes.
Thanks
The first thing that you have to do is to enable Spring Data web support. If you are using Spring Boot, it's probably activated by Spring Boot.
After you have enabled the Spring Data web support, you can specify the current page, page size, and sorting options by settings the values of these request parameters:
The value of the page request parameter identifies the current page (this is zero indexed).
The value of the size request parameter defines the page size. The default page size is 20.
The value of the sort request parameter defines the sort options.
Spring Data JPA reference manual provides more information about the supported syntax.
You can now "inject" the requested page information into your controller method by adding a new org.springframework.data.domain.Pageable method parameter into your controller method. In your case, the controller method could look as follows:
#RequestMapping(value = "/search", method=RequestMethod.POST)
public String search(#ModelAttribute("searchFilter") FilterDTO filter, Pageable page) {
//Add logic here
return "results";
}
I want to build a typical mvc app for CRUD of simple items, the api s should be RESTful. The catch here is, that i have a large pallete of items that needs to be initialized. On the server side those items are defined as java beans and the corresponding create form for the item is dynamically created from the field information(data type, validation constraints etc) harvested from the bean.
I am new to REST and just read up about how the urls should be nouns defining the resource and action specified by HTTP verb. In that perspective how to model something like
.../client/showForm?type=xyz from non RESTful way to RESTful one ?? My intention here is to tell the server to dynamically construct and send back a CREATE form for client of type xyz. The obvious problem with url i mentioned above is that it specifies action in the url which, from what i have read, makes it non RESTful.
When I think of REST, I think of resources. I think of data. In other words, I don't think of REST as being something that I would typically use to retrieve a form, which is a user interface component.
REST is an architectural style that is used to identify a resource on a server using a uniform resource identifier, or URI. Additionally, actions performed on those resources identified by the URI are determined based on the specific HTTP Method used in the request: GET, POST, PUT, DELETE, etc.
Thus, let's say you have a Client object. That client object might have the following properties:
Name
Location
AccountNumber
If I wanted to retrieve the data for a single client, I might use the following URI:
GET /client/xyz/ # xyx is the accountnumber used to identify the client.
I would use a GET method, since REST describes GET as being the method to use when retrieving data from the server.
The data could theoretically be returned in HTML, since REST is not a standard but more like a series of flexible guidelines; however, to really decouple my data from my user interface, I would choose to use something platform independent like JSON or XML to represent the data.
Next, when adding a client to the collection on the server, I would use the /client/ URI pattern, but I would use the HTTP Method POST, which is used when adding a resource to a collection on the server.
# Pass the data as JSON to the server and tell the server to add the client to the
# collection
POST /client/ {"accountnumber":"abc" , "Name" : "Jones" , "Location" : "Florida"}
If I were to modify an existing record on the server or replace it, I would most likely use the HTTP Method PUT, since REST guidelines say that PUT should be used if repeating the same operation repeatedly would not change the state of the server.
# Replace the client abc with a new resource
PUT /client/abc/ {"accountnumber":"abc" , "Name" : "Bob Jones" , "Location" : "Florida"}
The general idea behind REST is that it is used to identify a resource and then take action on that resource based on what HTTP Method is used.
If you insist on coupling your data with your view, one way accomplish this and retrieve the actual form, with the client data, could be to represent the form as a resource itself:
GET /client/abc/htmlform/
This URL would of course return your client data for client abc, but in an HTML form that would be rendered by the browser.
While my style of coding utilizes data transports such as JSON or XML to abstract and separate my data from my view, you could very well transport that data as HTML. However, the advantage of using JSON or XML is that your RESTful API becomes platform independent. If you ever expand your API to where other developers wish to consume it, they can do so, regardless of what specific platform or programming language they are using. In other words, the API could be used my PHP, Java, C#, Python, Ruby, or Perl developers.
In other words, any language or platform that can make HTTP requests and can send GET, POST, PUT, DELETE requests can be used to extend or build upon your API. This is the true advantage of REST.
For more information on setting up your controllers to use REST with Spring MVC, see this question. Additionally, check out the Spring MVC Documentation for more information.
Also, if you haven't checked out the Wikipedia article on REST, I strongly encourage you to do so. Finally, another good, classic read on REST is How I Explained REST To My Wife. Enjoy.