啟動(@SpringBootApplication)
/*SpringBoot項目的Bean裝配默認(rèn)規(guī)則是根據(jù)Application類所在的包位置從上往下掃描! “Application類”是指SpringBoot項目入口類。這個類的位置很關(guān)鍵*/
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebDemoApplication.class, args);
}
}
事務(wù)(@EnableTransactionManagement)
@SpringBootApplication
@EnableTransactionManagement
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebDemoApplication.class, args);
}
}
@Transactional
public interface BatchUserService {
ResponseObject batchRegisterLewUser(BatchRegisterReq req);
ResponseObject batchRegisterLiveUser(BatchRegisterReq req);
}
使用外置tomcat
@SpringBootApplication
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
thymeleaf (html)模板
<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#開發(fā)的時候 這里要設(shè)成false
spring.thymeleaf.cache=false
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
private static OkHttpClient client = new OkHttpClient();
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
/*以json格式向服務(wù)器發(fā)送post請求*/
public static String post(String url,JSONObject params) throws IOException {
RequestBody body = RequestBody.create(JSON,params.toJSONString());
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/*以formData(表格)形式向服務(wù)器發(fā)送post請求*/
public static String post(String url,JSONObject params) throws IOException {
RequestBody formBody = new FormBody.Builder()
.add("platform", "android")
.add("name", "bug")
.add("subject", "XXXXXXXXXXXXXXX")
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/*MultipartBody格式向服務(wù)器發(fā)送post請求*/
public static String post(String url,JSONObject params) throws IOException {
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "head_image", fileBody)
.addFormDataPart("imagetype", imageType)
.addFormDataPart("userphone", userPhone)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
Page