Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
SpringBoot
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Implementation== The standard implementation is heavily using a starter configuration provided by maven plugins. ===Folder Structure=== \target\ // contains the jar file of the Spring Boot application ===Package Structure=== There are two main approaches: structure by layer or structure by feature. It seems that a structure by feature is the most commonly used. ===Static Content=== * <code>\src\main\resources\static\</code> will be used in Spring Boot application by copying to <code>\target\classes\static\</code> * sources which have to be transpiled like Angular apps should not be located here but in a separate directory <code>\src\main\resources\<APP>\</code> * for Angular: ** adjust angular.json with "outputPath": "../../main/resources/static/", ===Main Application=== see <code>/test1/src/main/java/com/uweheuer/springboot/test1/Test1Application.java/</code> @SpringBootApplication public class Test1Application { ... public static void main(String[] args) { SpringApplication.run(Test1Application.class, args); } ... ====@SpringBootApplication==== is a convience annotation that adds <code>@Configuration</code>, <code>@EnableAutoConfiguration</code>, <code>@EnableWebMvc</code> and <code>@ComponentScan</code> which enables e.g. Auto-Configuration ===Rest Controller=== import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @CrossOrigin(origins="http://localhost:4200") // avoid cross origin errors in browser [[SpringBoot#.40RequestMapping|@RequestMapping]]("xyz") public class HelloWorldController { // @RequestMapping(method=RequestMethod.GET, path="/hello-world") @GetMapping(path="/hello-world") @JsonView(MenuView.NodeView.class) // tells Jackson to propagate the view class to the entities for [[Glossary#JSON|json]]-fication the return value // the same annotation has to be used at attributes in the JPA entities, which should be used for this view public <METHOD>() { ... } ====@RequestMapping==== import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("xyz") // no need of backslash, all URLs of the mappings have the prefix 'xyz', method mappings need to include the / after the prefix e.g. http://localhost/xyz/do_something public class XYZControler { @GetMapping("/do_something") public void doSomething() { ... ====@ResponseBody==== The <code>@ResponseBody</code> annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. There is no need to annotate the <code>@RestController</code>-annotated controllers with the <code>@ResponseBody</code> annotation since Spring does it by default. ====Method Return Values==== public User <METHOD> public ResponseEntity<User> <METHOD> // is the same as above ===[[JavaJEEJPA|JPA]]=== * Spring Boot does not need [[JavaJEEJPA#persistence.xml|persistence.xml]] ===JPA Repository=== import org.springframework.data.jpa.repository.JpaRepository; public interface TestRepository extends JpaRepository<E1, Long> { } // in controller @Autowired TestRepository testRepo; ====Repository==== <code>JpaRepository</code> extends <code>PagingAndSortingRepository</code> which in turn extends <code>CrudRepository</code>. Their main functions are: * CrudRepository mainly provides CRUD functions. * PagingAndSortingRepository provides methods to do pagination and sorting records. * JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch. Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository, use CrudRepository. =====Query Methods===== Define the query methods in a repository interface that extends one of the Spring Data's repositories. Spring Data JPA will create queries automatically by parsing these method names. A derived query method name has two main components separated by the first By keyword: * The '''introducer''' clause like find, read, query, count, or get which tells Spring Data JPA what you want to do with the method. This clause can contain further expressions, such as Distinct to set a distinct flag on the query to be created. * The '''criteria''' clause that starts after the first By keyword. The first By acts as a delimiter to indicate the start of the actual query criteria. The criteria clause is where you define conditions on entity properties and concatenate them with And and Or keywords. =====save()===== * can be called on new or existing entities * returns the entity, if new with the id set ====General Configuration==== * configuration in [[#Application_Properties|application.properties]] spring.jpa.hibernate.ddl-auto=[none|update|create|create-drop] // none: nothing is changed in the DB, default for MySQL // update: DB structure updated according to the entities // create: DB is created on startup // create-drop: creates DB on startup, drops it on closing, default for H2 ====MariaDB==== * in pom.xml: <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <scope>runtime</scope> </dependency> * configuration in [[#Application_Properties|application.properties]] spring.datasource.url=jdbc:mariadb://localhost:3306/test spring.datasource.username=root spring.datasource.password= ====MySQL==== * in pom.xml: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> * configuration in [[#Application_Properties|application.properties]] spring.datasource.url=jdbc:mysql://localhost:3306/mysql506 spring.datasource.username=root spring.datasource.password=mHalloo0@1m ===Command Line Runner=== CommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded. ===Integration Test=== @SpringBootTest class BookmarksApplicationTests { @Test void contextLoads() { } }
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width