TECH |
Techable(テッカブル) |
ChatGPT で スマホアプリ開発を効率化!「FlutterPrompts」リリース |
https://techable.jp/archives/199214
|
chatgpt |
2023-03-12 04:00:22 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
Pythonで株価データを取得し、グラフ化 |
https://qiita.com/hiro_hiro_0425/items/0654e2f5159527b89591
|
atplotlibpyplotaspltstart |
2023-03-12 13:31:29 |
Ruby |
Rubyタグが付けられた新着投稿 - Qiita |
【Rails】uniquenessバリデーションに気をつける話 |
https://qiita.com/koki_73/items/c8f511ebcc8be111786b
|
build |
2023-03-12 13:10:53 |
AWS |
AWSタグが付けられた新着投稿 - Qiita |
[AWS]SSLサーバー証明書の発行 |
https://qiita.com/tumu1632/items/2a6afc2c7b725899fd96
|
awsssl |
2023-03-12 13:37:12 |
Docker |
dockerタグが付けられた新着投稿 - Qiita |
ROSの勉強 第47弾:URDF: 独立差動二輪車の構築(設計~Gazebo導入まで) |
https://qiita.com/Yuya-Shimizu/items/1fd238211ed35d187e10
|
gazebo |
2023-03-12 13:36:05 |
GCP |
gcpタグが付けられた新着投稿 - Qiita |
GitLab Operatorを使用してGKEにGitLab環境を用意する |
https://qiita.com/nekoharuyuki/items/b1090675cd3af640077f
|
gkegooglekubernetese |
2023-03-12 13:21:08 |
Azure |
Azureタグが付けられた新着投稿 - Qiita |
AZURE リソース階層構造について超簡単に書いた |
https://qiita.com/takaikeee12/items/1fee2734cd4d8b2e6b05
|
azure |
2023-03-12 13:40:01 |
Git |
Gitタグが付けられた新着投稿 - Qiita |
GraphQLのバックエンド側の準備で詰まった時の話 |
https://qiita.com/clown6613/items/489f23fd898fa44dc584
|
graphql |
2023-03-12 13:32:45 |
Ruby |
Railsタグが付けられた新着投稿 - Qiita |
【Rails】uniquenessバリデーションに気をつける話 |
https://qiita.com/koki_73/items/c8f511ebcc8be111786b
|
build |
2023-03-12 13:10:53 |
海外TECH |
DEV Community |
Java CRUD Rest API using Spring Boot, Hibernate, Postgres, Docker and Docker Compose |
https://dev.to/francescoxx/java-crud-rest-api-using-spring-boot-hibernate-postgres-docker-and-docker-compose-5cln
|
Java CRUD Rest API using Spring Boot Hibernate Postgres Docker and Docker ComposeLet s create a CRUD Rest API in Java using Spring Boot Framework to build web servers in Java Hibernate Java ORM Postgres relational database Docker for containerization Docker Compose If you prefer a video version All the code is available in the GitHub repository link in the video description IntroHere is a schema of the architecture of the application we are going to create We will create endpoints for basic CRUD operations CreateRead allRead oneUpdateDeleteHere are the steps we are going through Create a Java application using Spring Boot Spring Web Spring Data and Hibernate Run the Postgres database in a container using Docker Compose and test it with TablePlus Dockerize the Java application writing a Dockerfile and a docker compose yml file to run the application and the database Build the Java App build the Docker image and run the container using Docker Compose then test it with Postman We will go with a step by step guide so you can follow along Requirements Java InstalledMaven or any other build tool for Java Any Java IDEDocker installed and running Optional Postman and Tableplus to follow along but any testing tool will work Create a new Java Spring Boot applicationYou can do this in multiple way If you use VSCode I d recommend to install these extensions Extension Pack for JavaSpring Initializr Java SupportIf you have these extensions you should see a similar prompt when you open VS Code Click on Create Java Project Then you should select how to build your Java project You can do this in different ways We will create this application by selecting in order Spring Boot Maven project you can choose Gradle if you want Spring Boot version any should work Java version any should work choose a package In my case com francescociulla javadockerArtifact Id live you can replace it with what you want and change the upcoming part accordingly packaging type Jar you can choose war if you want Java version Spring Web To handle HTTP requests Spring Data JPA To handle the database PostgreSQL Driver And finally Selected dependencies press to continue Now you should decide a folder to generate this project into In my case the path is c workspace but feel free to choose any path After generating it you should see this popup at the bottom right or you can simply open the folder with any IDE Click Open and you should have something like this Now an important part If we simply run the application if you have the extensions installed you should have a play button at the top right when you have the JavaApplication java file open like this it FAILSThis is not wrong because we want to connect this app to a Postgres DB but we don t have the DB Running In this case we will run the DB not by installing it but by using a Docker container Run the Postgres ContainerTo do that create a file called docker compose yml at the root level and populate it like that version services java db container name java db image postgres ports environment POSTGRES PASSWORD postgres POSTGRES USER postgres POSTGRES DB postgres volumes pgdata var lib postgresql datavolumes pgdata Explanation We are using version of the docker compose fileWe are creating a service called java db with the same container nameWe are using the image postgres you can use any version you want We are exposing the port of the container to the port of the hostWe are setting the password user and database nameWe are creating a volume to persist the data Now open a terminal and type docker compose up d java dbWait some seconds then type docker compose logsIf you have something like that with the database system is ready to accept connections you are good to go But to be sure let s make a test using TableplusOpen Tableplus and click on the close to search for connection and choose PostgreSQL Populate the fields with the following Host localhostPort User postgresPassword postgres don t use this in production Database postgresNow click the Test button at the bottom right If you get a connection is ok message as the following you are good to go you can also click connect and you will have an empty db Code the Java Spring Boot applicationTime to create our Spring Boot application In the main folder create a folder called userInside this folder create files User javaUserRepository javaUserController javaYour structure should look similar to this one Populate User java file package com example live user import jakarta persistence Entity Table name users public class User Id GeneratedValue strategy GenerationType AUTO private Long id Column name name private String name Column name email private String email getters and setters public Long getId return id public void setId Long id this id id public String getName return name public void setName String name this name name public String getEmail return email public void setEmail String email this email email Explanation This is a Java class that represents a User entity and is used in conjunction with a database using Jakarta Persistence previously known as Java Persistence API or JPA for object relational mapping ORM Here are the key features of this code The Entity annotation is used to mark this class as a JPA entity which means it can be persisted to a database table The Table annotation is used to specify the name of the database table to which this entity is mapped In this case it is mapped to a table named users The Id annotation is used to specify that the id field is the primary key of the entity The GeneratedValue annotation is used to specify that the value of the primary key should be generated automatically by the database system In this case the GenerationType AUTO strategy is used which means the database will choose the appropriate strategy based on the database being used The Column annotation is used to specify the name of the column in the database table that corresponds to the name and email fields If no name is specified the default is to use the name of the field The class has a constructor with no arguments and getters and setters for each of the fields Overall this class defines the schema of a database table called users which has columns for the user s ID name and email The Entity Table Id and Column annotations are used to specify how the class should be mapped to the database table while the getters and setters provide access to the data in the object Populate the UserRepository java file package com example live user import org springframework data jpa repository JpaRepository import org springframework stereotype Repository Repositorypublic interface UserRepository extends JpaRepository lt User Long gt Explanation of the code in UserRepository java This is a Java interface that defines a repository for the User entity using Spring Data JPA Here are the key features of this code The import statements at the top of the file bring in the necessary classes from the Spring Data JPA library The Repository annotation is used to mark this interface as a Spring bean that handles data access for the User entity The JpaRepository interface is extended by this interface This provides a set of methods for working with User entities such as saving deleting and finding users It also allows for pagination and sorting of results The UserRepository interface uses generics to specify the entity type and the type of its primary key In this case it works with User entities and Long primary keys Overall this interface provides a high level abstraction for working with User entities in a Spring Data JPA application By extending the JpaRepository interface and defining the User entity and primary key type Spring Data JPA will automatically generate the necessary implementation for the methods defined in the JpaRepository interface This allows developers to focus on defining the business logic for their application rather than writing boilerplate code for data access Populate the UserController java as following package com example live user import java util List import org springframework beans factory annotation Autowired import org springframework web bind annotation RequestMapping import org springframework web bind annotation RestController import org springframework web bind annotation DeleteMapping import org springframework web bind annotation GetMapping import org springframework web bind annotation PathVariable import org springframework web bind annotation PostMapping import org springframework web bind annotation RequestBody import org springframework web bind annotation PutMapping RestController RequestMapping api users public class UserController Autowired private UserRepository userRepository GetMapping public List lt User gt getAllUsers return userRepository findAll GetMapping id public User getUserById PathVariable Long id return userRepository findById id get PostMapping public User createUser RequestBody User user return userRepository save user PutMapping id public User updateUser PathVariable Long id RequestBody User user User existingUser userRepository findById id get existingUser setName user getName existingUser setEmail user getEmail return userRepository save existingUser DeleteMapping id public String deleteUser PathVariable Long id try userRepository findById id get userRepository deleteById id return User deleted successfully catch Exception e return User not found Explanation for the Usercontroller java code This is a Java class that defines a RESTful API for performing CRUD Create Read Update Delete operations on the User entity using Spring Boot and Spring Data JPA Here are the key features of this code The import statements at the top of the file bring in the necessary classes and annotations from the Spring Boot and Spring Data JPA libraries The RestController annotation is used to mark this class as a RESTful controller that handles HTTP requests and responses The RequestMapping annotation is used to specify the base URL for all API endpoints In this case it is set to api users The Autowired annotation is used to inject an instance of the UserRepository interface into this class There are several endpoint methods defined in this class each mapped to a specific HTTP method GET POST PUT DELETE and URL path getAllUsers is a GET method that returns a list of all users in the database by calling the findAll method on the UserRepository getUserById Long id is a GET method that takes an ID parameter in the URL path and returns a single user with that ID by calling the findById Long id method on the UserRepository createUser User user is a POST method that takes a JSON representation of a new user in the request body and saves it to the database by calling the save User user method on the UserRepository updateUser Long id User user is a PUT method that takes an ID parameter in the URL path and a JSON representation of an updated user in the request body It updates the user with the given ID by first retrieving it from the database using findById Long id and then setting its properties to those of the updated user before saving it using save User user on the UserRepository deleteUser Long id is a DELETE method that takes an ID parameter in the URL path and deletes the user with that ID from the database by calling the deleteById Long id method on the UserRepository Overall this class provides a complete set of RESTful API endpoints for managing User entities in a Spring Boot and Spring Data JPA application Now it s time to finish the configuration part then we are doneThis application properties file is a configuration file used by a Spring Boot application It defines properties that configure the application s data source and Hibernate the ORM Object Relational Mapping framework used for database persistence application properties fileThis file is located in the resources folder Populate the application properties file spring datasource url DATABASE URL spring datasource username DATABASE USERNAME spring datasource password DATABASE PASSWORD spring jpa hibernate ddl auto updatespring jpa properties hibernate dialect org hibernate dialect PostgreSQLDialectExplanation spring datasource url This property sets the URL of the database to which the application will connect The value is set using the DATABASE URL placeholder which will be set in the docker compose yml file spring datasource username This property sets the username to use when connecting to the database The value is set using the DATABASE USERNAME placeholder which will be set in the docker compose yml file spring datasource password This property sets the password to use when connecting to the database The value is set using the DATABASE PASSWORD placeholder which will be set in the docker compose yml file spring jpa hibernate ddl auto This property sets the strategy used by Hibernate to generate and update the database schema In this case the value is set to update which means that Hibernate will automatically create tables and update the schema as needed based on the entity classes in the application spring jpa properties hibernate dialect This property sets the Hibernate dialect to use for communicating with the database In this case the value is set to org hibernate dialect PostgreSQLDialect which is the dialect for PostgreSQL databases Overall this application properties file is used to configure the data source and ORM framework for a Spring Boot application that connects to a PostgreSQL database DockerizationNow to connect the db to the Spring App we need to Dockerize the app first then run it using Docker ComposeThis Dockerfile is used to create a Docker image for a Java application that is packaged as a JAR file At the application root level create a file called Dockerfile capital D and populate it like this FROM openjdk jdk alpineCOPY target live SNAPSHOT jar app jarENTRYPOINT java jar app jar Explanation for the code inside the DockerfileFROM openjdk jdk alpine This line specifies the base image to use for building the Docker image In this case it uses the openjdk jdk alpine image which is a lightweight version of the OpenJDK image that is optimized for running Java applications in a containerized environment COPY target live SNAPSHOT jar app jar This line copies the JAR file of the Java application from the target directory in the local file system to the root directory of the Docker image and renames it to app jar This assumes that the JAR file has already been built and packaged in the target directory of the local file system If you used a different artifactId for your project you will need to change the name of the JAR file accordingly ENTRYPOINT java jar app jar This line sets the command to run when a container is started from the Docker image In this case it specifies that the java command should be executed with the jar option and the JAR file to run is app jar Docker ComposeLet s add the java app service in the docker compose yml file we previously created version services new service java app java app container name java app image francescoxx java app build ports environment DATABASE URL jdbc postgresql java db postgres DATABASE USERNAME postgres DATABASE PASSWORD postgres depends on java db old service postgres java db container name java db image postgres ports environment POSTGRES USER postgres POSTGRES PASSWORD postgres POSTGRES DB postgres volumes pgdata var lib postgresql datavolumes pgdata Explanation for the java app service container name is the custom name of the container This is optional but I recommend using it because it makes it easier to find the container in the list of running containers image is the image tag name we want to assign I recommend using your dockerhub account username instead of francescoxx that s mine build is the build path of the Dockerfile In this case the Dockerfile and the docker compose file are in the same directory so we can just use the dot that means here in this folder ports is the list of ports we want to expose In this case we are exposing the port of the java app container The format is host port container port environment is a list of environment variables wee want to use In this case we have of them for the connection to the db with the username and the password of course use more secure password in production Also note that the connection is to java db Containers find each other if they are on the same network by the container name This is why is so important to define a container name at least for the database container depends on is to run this container after running the Postgres one But please note that by doing this the container ios just waiting for the container to start not the database application insisde it so it might fail at the first try we should need something called healthcheck to do it properly Create the Jar fileWe need to create the jar file of the Java application so we can copy it into the Docker image To create the jar file type mvn clean package DskipTestsNote we are skipping the tests here because otherwise the build process is gonna FAIL The reason is that the tests are trying to connect to the database but the environment variables we defined are outside the application logic Now build the Docker image by using this command docker compose buildIf you get an error because the target file doesn t exist check the name of your Java file in the target folder Run the Java AppTo run the Java application simply type docker compose up java appIf your output looks like the one below you are good to go just to be sure on another terminal typedocker ps aYou should see something like this Now it s time to test our application Test our applicationBefore we test our application let s go back to Tableplus and refresh using the button at the top right corner of the application or close and reopen the connection Now there is a table in the database This is because we Annotated the User Class with the Entity annotation and we synced the models with the database we also defined a line in the application properties to do this see the th line of the application properties file Now let s test this CRUD API application with Postman Test the applicationSince we don t have a test endpoint we can just make a GET request to localhost api users The response should be an empty array Create a userNow let s create a user making a POST request to localhost api users with the body below as a request body Let s make one more And a third one Get all the usersNow that we created users let s get all of them making a GET request to localhost api users Get one userTo get a single user you can just make a GET request appending the user number we want at the end of the urlFor example to get the user you can make a GET request to localhost api user Update one userTo update an existing user you need to provide the new body and the user you want to modify For example to update the user you can make a PUT request to localhost api users by providing a new body Delete one userTo Delete one user make a DELETE request appending the id of the user you want to delete For example to delete the user make a DELETE request o localhost api users Note that if we try to delete a user that doesn t exist we handle the error for example As final test let check again with TablePlus ConclusionWe made it We have built a CRUD rest API in Java using Spring Boot Dependencies Spring Web Spring Data JPA PostgreSQL connector HibernatePostgresDockerDocker compose This is just an example but you can use this as a starting point to build your own application If you prefer a video version All the code is available in the GitHub repository link in the video description That s all If you have any question drop a comment below Francesco |
2023-03-12 04:16:14 |
ニュース |
BBC News - Home |
Gary Lineker: BBC boss Tim Davie 'sorry' after sport disruption in Lineker row |
https://www.bbc.co.uk/news/uk-64918162?at_medium=RSS&at_campaign=KARANGA
|
coverage |
2023-03-12 04:45:03 |
ビジネス |
東洋経済オンライン |
実は信長に強気?姉川の戦いに見た家康の頑固さ 懇願する信長に対し、一歩も引かなかった家康 | 歴史 | 東洋経済オンライン |
https://toyokeizai.net/articles/-/658269?utm_source=rss&utm_medium=http&utm_campaign=link_back
|
大河ドラマ |
2023-03-12 14:00:00 |
コメント
コメントを投稿