Tag: spring

将Hibernate 5.2与Spring框架4.x集成

我正在使用最新稳定的Spring版本( 4.2.6.RELEASE )。 将Hibernate从5.1升级到5.2并将hibernate-entitymanager依赖更改为hibernate-core hibernate文档后: https : //github.com/hibernate/hibernate-orm/wiki/Migration-Guide—5.2 我没有收到任何编译错误,但我的所有测试都失败了这个堆栈跟踪: Jun 06, 2016 12:00:38 PM org.springframework.test.context.TestContextManager beforeTestMethod WARNING: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@548b7f67] to process ‘before’ execution of test method [public void com.rh.cores.architecture.tests.units.person.PersonTest.add_iranianNaturalPersonDateIsInvalid_preventSaving()] for test instance [com.rh.cores.architecture.tests.units.person.PersonTest@7f9e8421] org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode; at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at […]

如何将基于方法的安全性添加到Spring Boot项目?

我想为Spring Boot项目添加基于方法的安全性。 似乎我只需要添加PermissionEvaluator和MethodSecurityExpressionHandler bean,使用@EnableGlobalMethodSecurity(prePostEnabled = true)注释我的WebSecurityConfigurerAdapter ,使用@PreAuthorize(“isAuthenticated() and hasPermission(#param, ‘somePermissionName’)”) 。 但是在添加PermissionEvaluator bean之后 @Bean public PermissionEvaluator permissionEvaluator() { HelloPermissionEvaluator bean = new HelloPermissionEvaluator(); return bean; } 我得到一个IllegalArgumentException :“需要一个ServletContext来配置默认的servlet处理”: Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘defaultServletHandlerMapping’ defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method […]

没有为响应类型找到合适的HttpMessageConverter

使用spring,使用以下代码: List<HttpMessageConverter> messageConverters = restTemplate.getMessageConverters(); for(HttpMessageConverter httpMessageConverter : messageConverters){ System.out.println(httpMessageConverter); } ResponseEntity productList = restTemplate.getForEntity(productDataUrl,ProductList.class); 我明白了 org.springframework.http.converter.ByteArrayHttpMessageConverter@34649ee4 org.springframework.http.converter.StringHttpMessageConverter@39fba59b org.springframework.http.converter.ResourceHttpMessageConverter@383580da org.springframework.http.converter.xml.SourceHttpMessageConverter@409e850a org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@673074aa org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e3b79d3 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@52bb1b26 org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycopmany.ProductList] and content type [text/html;charset=UTF-8] pojo的片段: @XmlRootElement(name=”TheProductList”) public class ProductList { @XmlElement(required = true, name = “date”) private LocalDate […]

什么时候被称为spring beans destroy-method?

我在bean的“destroy-method”中放了一个sysout语句。 当我运行示例代码时,sysout没有得到输出。 这是否意味着破坏方法没有被调用? 测试类: package spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InitTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(“InitTestContext.xml”); InitTestBean bean = (InitTestBean)ctx.getBean(“InitTestBean”); bean.display(); } } 豆 package spring.test; public class InitTestBean { private String prop1; private String prop2; public InitTestBean(String prop1, String prop2) { System.out.println(“Instantiating InitTestBean”); this.prop1 = […]

LDAP:如何使用连接详细信息validation用户身份

我无法使用LDAP对用户进行身份validation。 我有以下细节: URL=ldap://10.10.10.10:389 LDAP BASE:DC=lab2,DC=ins LDAP Bind Account: CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins LDAP Bind Account Pw: secret 我可以使用上面的详细信息搜索sAMAccountName值,但是如何使用用户名和密码validation用户? 如果您按照我之前的问题进行操作,那么您将了解到,我已成功连接到LDAP服务器但无法对其进行身份validation。 用户进行身份validation: user: someusername password: somepwd 我无法使用’somepwd’连接到LDAP服务器,我应该如何使用someusername 。 我能够将给定用户搜索为sAMAccountName 。

如何使用spring java配置在单例bean中生成原型对象

这是我现在的工作正常。 它所做的只是一个返回项目对象数组的市场类: 我有上课的市场 class market { public ArrayList createItems(HashMap map) { ArrayList array = new ArrayList(); for (Map.Entry m : map.entrySet()) { Item item = new Item(); item.setName(m.key()); item.setValue(m.value()); array.add(item); } return array; } } class Item是带有getter和setter的名称和值的简单类 以下是我的配置文件的外观: @Configuration public class MarketConfig { @Bean public Market market() { return new Market(); } } 我想如何改变我的代码:(原因:我不想要 Item […]

具有动态构造函数值的spring bean

我需要在没有构造函数参数的情况下创建一个完整的Object。 像这样的东西 Class A { private final int timeOut public A(int timeout) { this.timeOut = timeout; } //… } 我希望这个Bean能够进行弹簧管理,以便我以后可以使用Spring AOP。 但是我的bean需要超时才能作为动态值传递 – 有没有办法创建一个弹簧托管bean,其动态值在构造函数中被注入?

如何处理在Spring MVC中呈现视图时抛出的exception?

我有一个Spring MVC应用程序,它使用FreeMarker作为View技术(但也许视图技术对我的问题并不重要)。 我需要拦截在请求期间可能抛出的所有exception。 我已经实现了HandlerExceptionResolver,但只有在控制器中发生exception时才会执行此解析程序。 但是当控制器返回ModelAndView并且在渲染视图时发生exception(因为找不到变量或类似的东西),则不会调用exception解析器,而是在浏览器窗口中获得堆栈跟踪。 我还尝试在控制器中使用exception处理程序方法,该方法返回视图并使用@ExceptionHandler对其进行注释,但这也不起作用(很可能是因为exception不会在控制器中抛出但在视图中)。 那么是否有一些Spring机制可以注册一个捕获视图错误的exception处理程序?

Spring MVC – REST服务中bean列表上的@Valid

在Spring MVC REST服务(json)中,我有一个像这样的控制器方法: @RequestMapping(method = RequestMethod.POST, value = { “/doesntmatter” }) @ResponseBody public List myMethod(@Valid @RequestBody List request, BindingResult bindingResult) { MyBean类具有beanvalidation注释的位置。 在这种情况下似乎没有进行validation,尽管它适用于其他控制器。 我不想将列表封装在dto中,这将改变json输入。 为什么没有对bean列表进行validation? 有哪些替代方案?

Spring MVC 4:“application / json”内容类型未正确设置

我有一个使用以下注释映射的控制器: @RequestMapping(value = “/json”, method = RequestMethod.GET, produces = “application/json”) @ResponseBody public String bar() { return “{\”test\”: \”jsonResponseExample\”}”; } 我返回一个有效的JSON字符串,但是,当我在浏览器中查看Chrome Dev Tools上的响应时,内容类型不是application/json而是纯text/html 。 为什么没有设置内容类型? 我的web.xml : Spring MVC Web Application dispatcher org.springframework.web.servlet.DispatcherServlet 1 default *.js default *.css dispatcher / contextConfigLocation /WEB-INF/dispatcher-servlet.xml org.springframework.web.context.ContextLoaderListener 我的dispatcher-servlet.xml : 使用WildFly 8.1作为我的应用服务器。