【java – Spring Tool Suite 3.5.0上的Spring Roo项目】教程文章相关的互联网学习教程文章

java – Spring MVC:如何返回自定义404 errorpages?【代码】

当找不到请求的资源时,我正在寻找一种在Spring 4中返回自定义404错误的简洁方法.对不同域类型的查询应该导致不同的错误页面. 这里有一些代码来表明我的意图(Meter是一个域类): @RequestMapping(value = "/{number}", method = RequestMethod.GET) public String getMeterDetails(@PathVariable("number") final Long number, final Model model) {final Meter result = meterService.findOne(number);if (result == null) {// her...

java – 在Spring Boot中以编程方式配置DataSource【代码】

使用Spring Boot,我可以使用以下代码实例化JdbcTemplate: 码:@Autowired private JdbcTemplate jdbcTemplate;属性:spring.datasource.url=jdbc:postgresql://my_url:my_port/my_other_stuff spring.datasource.username=my_user_name spring.datasource.password=my_password spring.datasource.driver-class-name=org.postgresql.Driver这将创建一个类的DataSource:org.apache.tomcat.jdbc.pool.DataSource 如何以编程方式设置...

java – Spring Boot 2.0迁移之后:driverClassName需要jdbcUrl【代码】

解决了:我的解决方案:我从DatabaseConfig.java中删除了dataSource()方法.然后,应用程序成功启动:) 我刚刚将Spring Boot项目从1.5.x更新到2.0.0.在更新之前,此应用程序正常工作但在更新后,我收到的一些错误如下.有什么问题,你能帮帮我吗? 我在项目中使用PostgreSQL,Hibernate,JPA. 我也尝试了this,但它对我不起作用. 谢谢你的时间 :) 这是问题:2018-03-03 23:19:37.934 ERROR 42323 --- [ main] com.zaxxer.hikari.H...

java – Spring Boot 2.0.x禁用某些配置文件的安全性【代码】

在Spring Boot 1.5.x中,我已经配置了安全性,并且在某些配置文件(例如本地)中,我已将security.basic.enabled = false行添加到.properties文件中以禁用该配置文件的所有安全性.我正在尝试迁移到新的Spring Boot 2,其中删除了该配置属性.如何在Spring Boot 2.0.x中实现相同的行为(不使用此属性)? 我已经读过Spring-Boot-Security-2.0和security-changes-in-spring-boot-2-0-m4,这个属性没什么.解决方法:您必须添加自定义Spring Secur...

java – Spring Boot REST API – 请求超时?【代码】

我有一个Spring Boot REST服务,有时会将第三方服务作为请求的一部分.我想在我的所有资源上设置一个超时(让我们说5秒),这样如果任何请求处理(整个链,从传入到响应)花费的时间超过5秒,我的控制器会响应HTTP 503而不是实际响应.如果这只是一个Spring属性,例如设置,那将是非常棒的spring.mvc.async.request-timeout=5000但我没有运气.我也尝试过扩展WebMvcConfigurationSupport并覆盖configureAsyncSupport:@Override public void con...

java – Spring @ResponseBody注释如何在这个RESTful应用程序示例中工作?【代码】

我有一个以下列方式注释的方法:/** * Provide a list of all accounts. */ // TODO 02: Complete this method. Add annotations to respond // to GET /accounts and return a List<Account> to be converted. // Save your work and restart the server. You should get JSON results when accessing // http://localhost:8080/rest-ws/app/accounts @RequestMapping(value="/orders", method=RequestMethod.GET) public ...

java – Spring Boot添加Http请求拦截器【代码】

在Spring启动应用程序中添加HttpRequest拦截器的正确方法是什么?我想要做的是为每个http请求记录请求和响应. Spring启动文档根本不涉及此主题. (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/) 我发现了一些关于如何对旧版本的spring执行相同操作的Web示例,但这些示例与applicationcontext.xml一起使用.请帮忙.解决方法:由于您使用的是Spring Boot,我认为您更愿意尽可能依赖Spring的自动配置.要添加其他...

java – Spring中的循环依赖

Spring如何解决这个问题:bean A依赖于bean B,而bean B依赖于bean A.解决方法:正如其他答案所说,Spring只是处理它,创建bean并根据需要注入它们. 其中一个后果是bean注入/属性设置可能以与XML布线文件似乎暗示的顺序不同的顺序发生.因此,您需要注意,您的属性设置器不会执行依赖于已调用的其他setter的初始化.处理此问题的方法是将bean声明为实现InitializingBean接口.这需要您实现afterPropertiesSet()方法,这是您进行关键初始化的地...

java – Spring Boot中的spring.jpa.open-in-view = true属性是什么?

我在Spring Boot文档中看到了JPA配置spring.jpa.open-in-view = true的这个属性.那么它被设置为true,它是默认值吗? 它到底意味着什么?我没有找到任何解释这种行为. 应该使用Hibernate SessionFactory而不是EntityManagerFactory吗?如果是,我如何设置为EntityManagerFactory? 感谢任何帮助. 谢谢!解决方法:此属性将注册一个OpenEntityManagerInViewInterceptor,它将EntityManager注册到当前线程,因此在Web请求完成之前,您将拥有...

java – Spring Boot – Hibernate SessionFactory的句柄【代码】

有谁知道如何处理由Spring Boot创建的Hibernate SessionFactory?解决方法:你可以用: SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); 其中entityManagerFactory是JPA EntityManagerFactory.package net.andreaskluth.hibernatesample;import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory;import org.hibernate.Session; import org.hibernate.Session...

java – Spring Boot – 如何在一个地方记录所有具有异常的请求和响应?【代码】

我正在用弹簧靴做休息api.我需要使用输入参数(使用方法,例如GET,POST等),请求路径,查询字符串,此请求的相应类方法,以及此操作的响应,成功和错误来记录所有请求. 举个例子: 成功要求:http://example.com/api/users/1日志应该看起来像这样:{HttpStatus: 200,path: "api/users/1",method: "GET",clientIp: "0.0.0.0",accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",method: "UsersController.getUser",arguments: {id: 1 },respons...

java – 为什么Spring MVC用404响应并报告“在DispatcherServlet中找不到带有URI […]的HTTP请求的映射”?【代码】

我正在编写一个部署在Tomcat上的Spring MVC应用程序.请参阅以下minimal, complete, and verifiable examplepublic class Application extends AbstractAnnotationConfigDispatcherServletInitializer {protected Class<?>[] getRootConfigClasses() {return new Class<?>[] { };}protected Class<?>[] getServletConfigClasses() {return new Class<?>[] { SpringServletConfig.class };}protected String[] getServletMappings() ...

java – Spring 5中lite @Bean方法的行为【代码】

从Spring 5 docsWhen @Bean methods are declared within classes that are not annotatedwith @Configuration they are referred to as being processed in a‘lite’ mode. Bean methods declared in a @Component or even in a plainold class will be considered ‘lite’, with a different primary purposeof the containing class and an @Bean method just being a sort of bonusthere. For example, service components m...

java – 在spring boot中增加tomcat的连接超时【代码】

如何增加超时以便在处理响应之前,请求不会超时? Spring Boot中的Tomcat设置 – server.tomcat.max-connections=2000 server.tomcat.max-threads=200 server.connection-timeout=1200000在15秒期间,每秒请求在(15)期间将constantUsersPerSec(20)提升到300并且所有请求都被提供,如下面的图表中可以看到的那样(蓝色).scn.inject(constantUsersPerSec(20) during (15), )这是由于max-connections = 2000,它使用200个工作线程提供300个...

java – Spring boot 2不适用于Postgres【代码】

我有一个Spring Boot JPA应用程序,带有spring boot 2.0.3.RELEASE并连接到PostgreSQL,当我运行应用程序时,我收到以下错误消息:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class数据库连接属性:spring:datasource:url: jdbc:postgresql://localhost:5432/testSusername: postgrespassword: postgres12...