Alibabacloud.com offers a wide variety of articles about restful web services with spring boot, easily find your restful web services with spring boot information here online.
------Error Handling mechanism------Default effect1 Returns a default error pageThe client responds to JSON data by defaultPrinciple: Automatic configuration with reference to errormvcautoconfiguration errorDefaulterrorattributesBasicerrorcontrollerErrorpagecustomizerDefaulterrorviewresolverSteps:Once the system has an error: Errorpagecustomizer effective (custom error response rule)2 How to customize the error response1) Custom error page2) Custom JSON error data
;Importcom.cfj.testboot.domain.UserDo; @RestController Public classTestreturnjson {@RequestMapping ("/returnjson") PublicUserdo Testreturnjson () {Userdo u=NewUserdo (); U.setid (1); U.settname ("Testers"); returnu; }}The results of the operation are as follows:@RestController annotations, equivalent to @[email protected] Two annotations, the return JSON data does not need to be preceded by the method @responsebody annotations, but using @restcontroller this annotation, Cannot return jsp,htm
=requestmethod.get) PublicModelandview Doview () {Modelandview mv=NewModelandview ("Index"); returnMV; }}3. Start with the application [main] method4. Access the URL, access to the JSP pageHttp://localhost:8080/initpageAlso encountered a problem:
Spring boot returns the JSP file download directly.
Reason:The JSP file is not parsed, just add the following configuration in the Pom.xml file. OK,
Spring Boot official provides a clear and understandable example of landing authentication,In this example, we will demonstrate how spring boot can be certified for login.First we go to https://start.spring.io/download a sample project Spring-test.Our example contains three
I have no words, the DAO interface to re-modify, run again.
DAO interface
PackageSite.wangxin520.springboot.dao;ImportOrg.apache.ibatis.annotations.Delete;ImportOrg.apache.ibatis.annotations.Mapper;Importorg.apache.ibatis.annotations.Select; @Mapper Public InterfaceIndexmapper {//@Select ("Select username from ' user ' WHERE id=#{id};")@Select ("Select username from user where username like #{id};") Publicstring GetName (string id); //@Select ("DELETE from ' user ' where ID =
In the previous article, we used a single data source connection to access, insert data, but often as the volume of business development, we usually do the database split or introduce other databases, so we need to configure multiple data sources, The following is based on the previous jdbctemplate and SPRING-DATA-JPA examples of how the two multiple data sources are configured.
First look at the jar referenced by the pom file
?XML version= "1.0" enc
Spring boot starts the web, embedded tomcat, springtomcat
1. Configure the pom. xml file (as appropriate)
2. entity class User
package com.nnk.maven.test.entity;/** * */public class User {private Long id;private String name;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(Stri
parent label1.3. 0. M2Finally add the module dependency can beAs for the use of the module, as long as the dependency is written in the line, do not need other special action, use the example click here to have a video show, or pretty clear.Pay special attention to Because the inheritance of the Pom.xml version is 1.3.0.m2, the original dependent on the inside if you omit version, but in the 1.3.0.m2 without the module will be error, this time only need to manually add the dependent version co
We are in the Web project, have encountered 404,405,500 such error message, need to handle the exception information,
Launch the app, access a nonexistent URL, or modify the processing to throw an exception directly, such as:
1
@RequestMapping ("/hello")
2
Public String Hello () throws Exception {
3
throw new Exception ("error occurred");
4
}
The
We have encountered 404,405,500 of these error messages in our Web project, and we need to handle exception information.
Start the application, access a non-existent URL, or modify the processing content to throw an exception directly, such as:
1
@RequestMapping ("/hello")
2
Public String Hello () throws Exception {
3
throw new Exception ("error occurred");
4
1. Project The Main method class is the project that launches the class startup project itself integrated Tomcat2.Controller Control LayerConfiguration Files Application.yml DataSource and JPA are equivalent to public configurationAssignment of a class3.AOP and spring AOP are pretty much the same XML that doesn't need to be configured4. Unified exception handling unification is the same formatThis exception is thrown directly on the controller to call
Framework project is as follows: Com.baidu.projectname Engineering's artifact generally directly fill in the project name dependencies are important, and our project uses the following dependencies:
Web: Contains all the things Spring Mvc,tomcat needs (Spring boot is based on
It takes only two steps to replace the default Web container in Springboot as jetty
Step One: Remove the default tomcat dependencies in the Springboot.
Step Two: Add jetty dependencies.
Completing the POM file:
Project Launch results show:
---: jetty-9.4.7.v20170914
---: Defaultsessionidmanager workername=node0
---: No sessionscavenger set, using D Efaults
---: scavenging every 660000ms
---: Initializing Spr
The Web Development Rendering page of Spring boot Learning (iv) was written in the previous article--Freemarker
Next, let's talk about velocity integration, which is the same as Freemarker, no big difference .
Here's a look at Pol. The jar introduced by XML
Need to
1
2
3
The Spring boot hit jar POM configuration is as follows: note Spring-boot-maven-plugin version problem
My development tool is using idea click Maven Project to find the input Maven command window as follows:
Click the button above with M to enter:
Clean:clean PACKAGE-DMAVEN.TEST.SKIP=TRUE-E
Locate the jar in th
The test file can be automatically generated in the class file by right-->go to->test1. Add Test NotesSimple method Test@RunWith (Springrunner.class)@SpringBootTestpublic class Girlservicetest { @Autowired Private Girlservice Girlservice; @Test public void FindOne () throws Exception { Girl Girl = Girlservice.findone (30); Assert.assertequals (New Integer, Girl.getage ()); Assertion }}RESTAPI Test@RunWith (Springrunner.class)@SpringBootTest@AutoConfigureMockMvcpublic
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.