高台县建设局网站,wordpress商店展示,环球资源网是卖什么的,郑州鹏之信网络科技有限公司web开发 spring boot web开发非常的简单#xff0c;其中包括常用的json输出、filters、property、log等 json 接口开发 在以前的spring 开发的时候需要我们提供json接口的时候需要做那些配置呢 就这样我们会经常由于配置错误#xff0c;导致406错误等等#xff0c;spring bo…web开发 spring boot web开发非常的简单其中包括常用的json输出、filters、property、log等 json 接口开发 在以前的spring 开发的时候需要我们提供json接口的时候需要做那些配置呢 就这样我们会经常由于配置错误导致406错误等等spring boot如何做呢只需要类添加 RestController 即可默认类中的方法都会以json的格式返回 如果我们需要使用页面开发只要使用 Controller 下面会结合模板来说明 自定义Filter 我们常常在项目中会使用filters用于录调用日志、排除有XSS威胁的字符、执行权限验证等等。Spring Boot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter并且我们可以自定义Filter。 1 Configuration2 public class WebConfiguration {3 4 Bean5 6 public RemoteIpFilter remoteIpFilter() {7 8 return new RemoteIpFilter();9
10 }
11
12 Bean
13
14 public FilterRegistrationBean testFilterRegistration() {
15
16 FilterRegistrationBean registration new FilterRegistrationBean();
17
18 registration.setFilter(new MyFilter());
19
20 registration.addUrlPatterns(/*);
21
22 registration.addInitParameter(paramName, paramValue);
23
24 registration.setName(MyFilter);
25
26 registration.setOrder(1);
27
28 return registration;
29
30 }
31
32 public class MyFilter implements Filter {
33
34 Override
35
36 public void destroy() {
37
38 // TODO Auto-generated method stub
39
40 }
41
42 Override
43
44 public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
45
46 throws IOException, ServletException {
47
48 // TODO Auto-generated method stub
49
50 HttpServletRequest request (HttpServletRequest) srequest;
51
52 System.out.println(this is MyFilter,url :request.getRequestURI());
53
54 filterChain.doFilter(srequest, sresponse);
55
56 }
57
58 Override
59
60 public void init(FilterConfig arg0) throws ServletException {
61
62 // TODO Auto-generated method stub
63
64 }
65
66 }
67
68 } 自定义Property 在web开发的过程中我经常需要自定义一些配置文件如何使用呢 配置在application.properties中 log配置 配置输出的地址和输出级别 数据库操作 在这里我重点讲述mysql、spring data jpa的使用其中mysql 就不用说了大家很熟悉jpa是利用Hibernate生成各种自动化的sql如果只是简单的增删改查基本上不用手写了spring内部已经帮大家封装实现了。 下面简单介绍一下如何在spring boot中使用 1、添加相jar包 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency 2、添加配置文件 spring.datasource.urljdbc:mysql://localhost:3306/test
spring.datasource.usernameroot
spring.datasource.passwordroot
spring.datasource.driver-class-namecom.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.autoupdate
spring.jpa.properties.hibernate.dialectorg.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql true 其实这个hibernate.hbm2ddl.auto参数的作用主要用于自动创建|更新|验证数据库表结构,有四个值 dialect 主要是指定生成表名的存储引擎为InneoDBshow-sql 是否打印出自动生产的SQL方便调试的时候查看 3、添加实体类和Dao Entity
public class User implements Serializable {private static final long serialVersionUID 1L;IdGeneratedValueprivate Long id;Column(nullable false, unique true)private String userName;Column(nullable false)private String passWord;Column(nullable false, unique true)private String email;Column(nullable true, unique true)private String nickName;Column(nullable false)private String regTime;//省略getter settet方法、构造方法} dao只要继承JpaRepository类就可以几乎可以不用写方法还有一个特别有尿性的功能非常赞就是可以根据方法名来自动的生产SQL比如findByUserName 会自动生产一个以 userName 为参数的查询方法比如 findAlll 自动会查询表里面的所有数据比如自动分页等等。。 Entity中不映射成列的字段得加Transient 注解不加注解也会映射成列 public interface UserRepository extends JpaRepositoryUser, Long {User findByUserName(String userName);User findByUserNameOrEmail(String username, String email); 4、测试 RunWith(SpringJUnit4ClassRunner.class)
SpringApplicationConfiguration(Application.class)
public class UserRepositoryTests {Autowiredprivate UserRepository userRepository;Testpublic void test() throws Exception {Date date new Date();DateFormat dateFormat DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String formattedDate dateFormat.format(date);userRepository.save(new User(aa1, aa126.com, aa, aa123456,formattedDate));userRepository.save(new User(bb2, bb126.com, bb, bb123456,formattedDate));userRepository.save(new User(cc3, cc126.com, cc, cc123456,formattedDate));Assert.assertEquals(9, userRepository.findAll().size());Assert.assertEquals(bb, userRepository.findByUserNameOrEmail(bb, cc126.com).getNickName());userRepository.delete(userRepository.findByUserName(aa1));}} 当让 spring data jpa 还有很多功能比如封装好的分页可以自己定义SQL主从分离等等这里就不详细讲了 thymeleaf模板 Spring boot 推荐使用来代替jsp,thymeleaf模板到底是什么来头呢让spring大哥来推荐下面我们来聊聊 Thymeleaf 介绍 Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSPVelocityFreeMaker等它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面而不需要启动整个Web应用。 好了你们说了我们已经习惯使用了什么 velocity,FreMakerbeetle之类的模版那么到底好在哪里呢 比一比吧 Thymeleaf是与众不同的因为它使用了自然的模板技术。这意味着Thymeleaf的模板语法并不会破坏文档的结构模板依旧是有效的XML文档。模板还可以用作工作原型Thymeleaf会在运行期替换掉静态值。Velocity与FreeMarker则是连续的文本处理器。 下面的代码示例分别使用Velocity、FreeMarker与Thymeleaf打印出一条消息 Velocity: p$message/p
FreeMarker: p${message}/p
Thymeleaf: p th:text${message}Hello World!/p ** 注意由于Thymeleaf使用了XML DOM解析器因此它并不适合于处理大规模的XML文件。** URL URL在Web应用模板中占据着十分重要的地位需要特别注意的是Thymeleaf对于URL的处理是通过语法{…}来处理的。Thymeleaf支持绝对路径URL 页面即原型 在Web开发过程中一个绕不开的话题就是前端工程师与后端工程师的写作在传统Java Web开发过程中前端工程师和后端工程师一样也需要安装一套完整的开发环境然后各类Java IDE中修改模板、静态资源文件启动/重启/重新加载应用服务器刷新页面查看最终效果。 但实际上前端工程师的职责更多应该关注于页面本身而非后端使用JSPVelocity等传统的Java模板引擎很难做到这一点因为它们必须在应用服务器中渲染完成后才能在浏览器中看到结果而Thymeleaf从根本上颠覆了这一过程通过属性进行模板渲染不会引入任何新的浏览器不能识别的标签例如JSP中的不会在Tag内部写表达式。整个页面直接作为HTML文件用浏览器打开几乎就可以看到最终的效果这大大解放了前端工程师的生产力它们的最终交付物就是纯的HTML/CSS/JavaScript文件。 Gradle 构建工具 spring 项目建议使用Gradle进行构建项目相比maven来讲 Gradle更简洁而且gradle更时候大型复杂项目的构建。gradle吸收了maven和ant的特点而来不过目前maven仍然是Java界的主流大家可以先了解了解。 一个使用gradle配置的项目 buildscript {repositories {maven { url http://repo.spring.io/libs-snapshot }mavenLocal()}dependencies {classpath(org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE)}
}apply plugin: java //添加 Java 插件, 表明这是一个 Java 项目
apply plugin: spring-boot //添加 Spring-boot支持
apply plugin: war //添加 War 插件, 可以导出 War 包
apply plugin: eclipse //添加 Eclipse 插件, 添加 Eclipse IDE 支持, Intellij Idea 为 ideawar {baseName favoritesversion 0.1.0
}sourceCompatibility 1.7 //最低兼容版本 JDK1.7
targetCompatibility 1.7 //目标兼容版本 JDK1.7repositories { // Maven 仓库mavenLocal() //使用本地仓库mavenCentral() //使用中央仓库maven { url http://repo.spring.io/libs-snapshot } //使用远程仓库
}dependencies { // 各种 依赖的jar包compile(org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE)compile group: mysql, name: mysql-connector-java, version: 5.1.6compile group: org.apache.commons, name: commons-lang3, version: 3.4compile(org.springframework.boot:spring-boot-devtools:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-test:1.3.6.RELEASE)compile org.webjars.bower:bootstrap:3.3.6compile org.webjars.bower:jquery:2.2.4compile(org.webjars:vue:1.0.24)compile org.webjars.bower:vue-resource:0.7.0}bootRun {addResources true
} WebJars WebJars是一个很神奇的东西可以让大家以jar包的形式来使用前端的各种框架、组件。 什么是WebJars 什么是WebJarsWebJars是将客户端浏览器资源JavaScriptCss等打成jar包文件以对资源进行统一依赖管理。WebJars的jar包部署在Maven中央仓库上。 为什么使用 我们在开发Java web项目的时候会使用像MavenGradle等构建工具以实现对jar包版本依赖管理以及项目的自动化管理但是对于JavaScriptCss等前端资源包我们只能采用拷贝到webapp下的方式这样做就无法对这些资源进行依赖管理。那么WebJars就提供给我们这些前端资源的jar包形势我们就可以进行依赖管理。 如何使用 1、 WebJars主官网 查找对于的组件比如Vuejs 转载于:https://www.cnblogs.com/tanlei-sxs/p/9487776.html