Tag: spring boot

unit testingSpring MissingServletRequestParameterException JSON响应

我在Spring引导rest控制器中有POST方法,如下所示 @RequestMapping(value=”/post/action/bookmark”, method=RequestMethod.POST) public @ResponseBody Map bookmarkPost( @RequestParam(value=”actionType”,required=true) String actionType, @RequestParam(value=”postId”,required=true) String postId, @CurrentUser User user) throws Exception{ return service.bookmarkPost(postId, actionType, user); } 现在,如果我在Postman中测试缺少参数,我会获得400个http响应和一个JSON正文: { “timestamp”: “2015-07-20”, “status”: 400, “error”: “Bad Request”, “exception”: “org.springframework.web.bind.MissingServletRequestParameterException”, “message”: “Required String parameter ‘actionType’ is not present”, “path”: “/post/action/bookmark” } 直到现在它没关系,但是当我尝试进行unit testing时,我没有得到JSON响应 @Test public void bookmarkMissingActionTypeParam() throws Exception{ // @formatter:off […]

使用MyBatis的Spring:期望的单个匹配bean但找到2

我一直在使用Spring和MyBatis,它对单个数据库的效果非常好。 我在尝试添加另一个数据库时遇到了困难(请参阅Github上的可重复示例 )。 我正在使用Spring Java配置(即不是XML)。 我见过的大多数示例都展示了如何使用XML实现这一点。 我有两个数据配置类(A和B),如下所示: @Configuration @MapperScan(“io.woolford.database.mapper”) public class DataConfigDatabaseA { @Bean(name=”dataSourceA”) public DataSource dataSourceA() throws SQLException { SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriver(new com.mysql.jdbc.Driver()); dataSource.setUrl(“jdbc:mysql://” + dbHostA + “/” + dbDatabaseA); dataSource.setUsername(dbUserA); dataSource.setPassword(dbPasswordA); return dataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSourceA()); return sessionFactory.getObject(); } […]

Spring Boot从命令行执行将文件添加到classpath

我正在使用Netbeans 8.2来开发Spring应用程序。 我遇到问题的这个特定应用程序是一个Spring Boot 1.5.3应用程序。 我有一个spring xml文件和一个application.properties,我保存在根项目目录下的/ config中。 我通过@ImportResource注释和一个值属性(如@ImportResource(value=”${config.xmlfile}”)将spring xml文件传递给我的项目。 当我单击Netbeans中的“运行项目”按钮时,我的Spring应用程序启动并正确找到我的/ config文件夹中的application.properties文件。 但是,对该文件夹中其他文件的任何类路径引用都将丢失。 例如,将config.xml文件设置为classpath:config/file.xml或classpath:file.xml都无法找到文件,但file:config/file.xml无效。 同样,从命令行运行时,我的结构如下: app/ |– bin | `– app-run.sh |– config | |– application.properties | |– log4j2.xml | |– file.xml `– app-exec.jar 我使用spring-boot-maven-plugin来制作如下jar: org.springframework.boot spring-boot-maven-plugin ${spring.boot.version} repackage exec 我的app-run.sh脚本执行以下操作: exec /bin/java -cp :.:../config/*:../app-exec.jar -Dlogging.config=../config/log4j2.xml -Dspring.config.location=../config/application.properties -jar ../app-exec.jar 其中/ bin / java表示我安装java的位置。 在-cp中设置的类路径似乎不起作用。 与通过IDE运行时类似,将config.xml文件设置为classpath:config […]

是否有另一种方法可以在Spring Boot中获取WebServiceTemplate而不是WebServiceGatewaySupport#getWebServiceTemplate()?

Spring提供了org.springframework.ws.client.core.support.WebServiceGatewaySupport类,它是根据Spring文档 a 方便的超类,适用于需要Web服务访问的应用程序类。 该课程显然是为扩展而设计的 。 它是abstract所以它不能被实例化为bean,所以我不能使用组合而不是inheritance 。 但是,当我inheritance该类时,Spring开始抱怨: [WARN] org.springframework.framework.CglibAopProxy – 无法代理接口实现方法 [public final void org.springframework.ws.client.core.support.WebServiceGatewaySupport.afterPropertiesSet()throws java.lang.Exception] 因为它被标记最后 : 考虑使用基于接口的JDK代理! 各种资源(例如https://github.com/spring-projects/spring-boot/issues/8974 )说的像 如果您实际上没有通过代理调用xxx方法,则可以安全地忽略该警告 但是我很高兴在申请中发出任何警告。 习惯忽略警告通常不好,而且我们的一些客户坚持“零警告政策”。 我们只需要该类来获取WebServiceTemplate 。 我们正在以某种方式使用它: response = getWebServiceTemplate().marshalSendAndReceive( uri, request, new SoapActionCallback(soapAction)); 题: 在Spring中是否有另一种方法(实际上,我们使用Spring Boot )来实现相同的结果,而无需额外的配置和没有警告 ? 也许Spring有另一个具有相同function的类用于组合? 还是静态工厂方法?

SpringBoot应用程序一直在重启(重启循环) – spring.devtools

我有一个带有嵌入式tomcat的spring boot应用程序,并且如果在类路径中发生了某些变化,则使用spring-boot-devtools重新启动应用程序。 我的IDE是Spring Tool Suite,我切换了“自动构建”,因为我认为这可能会在后台更改文件,从而触发重启 我的问题是,在tomcat和应用程序启动之后,它立即在无限循环中重启所有内容: 2017-08-22 10:24:04.309 INFO 9772 — [ restartedMain] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 8055 (http) 2017-08-22 10:24:04.415 DEBUG 9772 — [ restartedMain] osboot.devtools.restart.Restarter : Creating new Restarter for thread Thread[main,5,main] 2017-08-22 10:24:04.417 DEBUG 9772 — [ restartedMain] osboot.devtools.restart.Restarter : Immediately restarting application 2017-08-22 10:24:04.418 DEBUG 9772 — [ restartedMain] […]

Spring Boot忽略@JsonDeserialize和@JsonSerialize

我有一个带有RESTful端点的Spring Boot应用程序,我想为joda-time添加自定义序列化程序,但我无法让应用程序默认Jackson serailzier识别我的自定义端口。 我使用@RepositoryRestResource创建了RESTFul端点 @RepositoryRestResource(collectionResourceRel = “x”, path = “x”) public interface XRepository extends PagingAndSortingRepository { } 然后我有一个GET调用来返回所有对象X: HTTP://本地主机:8181 / X 这是我的序列化器: @Component public class JsonDateSerializer extends JsonSerializer { private static DateTimeFormatter formatter = DateTimeFormat.forPattern(“dd/MM/yyyy”); @Override public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.print(value)); } } 我将它添加到属性Getter,如下所示: @JsonSerialize(using=JsonDateSerializer.class) public DateTime […]

将请求范围的bean注入另一个bean

我想创建一个在请求生命周期中唯一的UUID。 为此,我使用@Scope(“request”)注释创建一个UUID bean。 @Bean @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST) public UUID requestUUID() { return UUID.randomUUID(); } 我想在我的控制器中访问这个bean。 所以我用@Autowired注入它。 这很好用。 @Controller public class DashboardController { @Autowired UUID uuid; @Autowired WelcomeMessageService welcomeMessageService; @Autowired IssueNotificationService issueNotificationService; @RequestMapping(“/”) public String index(Model model) throws InterruptedException, ExecutionException { System.out.println(uuid); PortalUserDetails userLog = getPortalUserDetails(); BusinessObjectCollection welcomeMessages = welcomeMessageService.findWelcomeMessages( 20, 0, userLog.getZenithUser(), userLog.getConnectionGroup().getConnectionGroupCode(), “FR”); if(welcomeMessages!=null) […]

Spring启动+ Spring数据JPA +二级缓存给出mutate错误

我正在使用Spring Boot和Spring数据JPA获得多个数据源的应用程序。 我得到以下exceptionhibernate.cache.region.factory_class没有设置,虽然我在代码中设置它。 Spring Boot在我设置之前检查它,或者无法读取我的设置。 caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, eg) is available on the classpath. at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:83) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:364) […]

Spring Boot – 从Application.properties填充列表/集合?

这可能是一个愚蠢的问题,但是可以从Spring Boot中的application.properties文件填充列表。 这是一个简单的例子: public class SomeClass { @Value(“${hermes.api.excluded.jwt}”) private List excludePatterns = new ArrayList(); // getters/settings …. } application.properties // Is something along these lines possible???? hermes.api.excluded.jwt[0]=/api/auth/ hermes.api.excluded.jwt[1]=/api/ss/ 我知道我可以爆炸一个逗号分隔的字符串,但我只是好奇,如果有一个原生的春季启动方式来做到这一点?

如何在Spring Boot应用程序上启用Bearer身份validation?

我想要实现的是: 存储在通过jdbc访问的数据库(即MySQL)中的用户,权限,客户端和访问令牌 API公开了端点,让您问“我可以拥有OAuth2不记名令牌吗?我知道客户端ID和密码” 如果您在请求标头中提供Bearer令牌,则API允许您访问MVC端点 我对此非常了解 – 前两点正在发挥作用。 我无法为我的Spring Boot应用程序使用完全默认的OAuth2设置,因为标准表名已经在我的数据库中使用(例如,我已经有一个“用户”表)。 我手动构建了自己的JdbcTokenStore,JdbcClientDetailsS​​ervice和JdbcAuthorizationCodeServices实例,将它们配置为使用我的数据库中的自定义表名,并设置我的应用程序以使用这些实例。 所以,这就是我到目前为止所拥有的。 我可以要求持票人令牌: # The `-u` switch provides the client ID & secret over HTTP Basic Auth curl -u8fc9d384-619a-11e7-9fe6-246798c61721:9397ce6c-619a-11e7-9fe6-246798c61721 \ ‘http://localhost:8080/oauth/token’ \ -d grant_type=password \ -d username=bob \ -d password=tom 我收到回复; 太好了! {“access_token”:”1ee9b381-e71a-4e2f-8782-54ab1ce4d140″,”token_type”:”bearer”,”refresh_token”:”8db897c7-03c6-4fc3-bf13-8b0296b41776″,”expires_in”:26321,”scope”:”read write”} 现在我尝试使用该令牌: curl ‘http://localhost:8080/test’ \ -H “Authorization: Bearer 1ee9b381-e71a-4e2f-8782-54ab1ce4d140” 唉: { “timestamp”:1499452163373, “status”:401, […]