Tag: spring mvc

Spring MongoDB和Apache Shiro

我试图使用Apache Shiro与Spring和MongoDB。 我正在使用自动assembly的Spring Data Repositories。 我为Shiro创建了自己的自定义域,它使用Spring Data存储库与Mongo交谈: public class PlatformRealm extends AuthorizingRealm { @Autowired(required = true) protected UserRepository userRepository = null; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { … } } 我看到的问题是userRepository没有自动assembly。 我在控制台输出中得到以下行,引用PlatformRealm: INFO org.springframework.web.context.support.XmlWebApplicationContext – Bean ‘platformRealm’ of type [class com.resonance.platform.core.security.PlatformRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not […]

Spring java.lang.IllegalStateException:在提交响应后无法创建会话

我在我的spring应用程序中遇到会话管理问题,这是方案。 当用户打开我的应用程序URL时,它会要求提供凭据并登录。用户进入后,如果他打开一个新选项卡并粘贴我的应用程序URL,它将再次请求凭据并且用户登录。 现在,如果用户在tab1中注销,并且如果用户想要在第二个选项卡中执行任何操作,则用户会在下面的堆栈跟踪中收到错误并注销。 Oct 10, 2014 3:11:27 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [CollPortal] in context with path [/CollPortal] threw exception java.lang.IllegalStateException: Cannot create a session after the response has been committed at org.apache.catalina.connector.Request.doGetSession(Request.java:2886) at org.apache.catalina.connector.Request.getSession(Request.java:2316) at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:898) at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:910) at com.dc.core.common.FlashRecyclingFilter.doFilterInternal(FlashRecyclingFilter.java:22) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at com.dc.core.common.StripJSessionIdFilter.doFilter(StripJSessionIdFilter.java:101) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) […]

使用Spring显示JSP上的值列表

我想在我的jsp视图中显示我的值列表,但我无法做到这一点。 这是我的控制器类,它只是将List添加到ModelAndView映射,而不是重定向到我的index.jsp页面。 EmployeeController @Controller public class EmployeeController { @RequestMapping(value={“/employee”}, method = RequestMethod.GET) public String listEmployee(){ System.out.println(“Kontroler EmployeeController”); LinkedList list = getList(); ModelAndView map = new ModelAndView(“index”); map.addObject(“lists”, list); return map.getViewName(); } private LinkedList getList(){ LinkedList list = new LinkedList(); list.add(“Item 1”); list.add(“Item 2”); list.add(“Item 3”); return list; } } 的index.jsp Welcome to Spring Web MVC […]

如何在spring boot中启用浏览器缓存

我正试图让Spring启动让浏览器缓存静态资源。 我的资源位于“静态”下的类路径中。 当我查看发回的标头时,我看到修改标头设置正常,但不知何故,标题“Cache-Control:no-store”也被添加。 HTTP/1.1 200 Last-Modified: Wed, 24 Aug 2016 08:50:16 GMT Cache-Control: no-store Accept-Ranges: bytes Content-Type: text/css Content-Length: 434554 Date: Wed, 24 Aug 2016 09:42:42 GMT 我已经看到了这个答案如何在Spring Boot中启用HTTP响应缓存 ,但这似乎并不适用于我,因为我没有使用spring-security,它不在类路径上。 我正在使用带有百里香的sp​​ring-boot 1.4.0。 那么,如何让spring boot不包含Cache-Control头?

Spring @Scheduled注释

如何动态地使用Spring的@Scheduled注释? CronTrigger(String expression, TimeZone timeZone) http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronTrigger.html#CronTrigger-java.lang.String-java.util.TimeZone- 由于我在数据库中有多个时区,我该如何动态传递它们? 我在我的代码中试过这个: TimeZone timezone = null; String timezone1 = null; public SchedulerBean(String timezone2) { this.timezone1 = timezone2; //constructor } @Scheduled(cron=”0 0 8 * * ?”, zone =timezone.getTimeZone(timezone1) ) //Error at this line public void sendQuestionNotif() { //……code } 这是我得到的错误 , *Type mismatch: cannot convert from TimeZone to String* 请帮帮我。 […]

从ServletOutputStream输出GIF

我正在编写一个动态生成GIF文件的端点。 我会从头开始。 我有一个名为Function的类,它的工作方式类似于抽象类,我有几个类,在本例中是AddFunction ,代表了一小部分function。 在这种情况下, AddFunction将一些数字加在一起。 当命中终点时, AddFunction的ID被传递给它(它可以是任何,在这个例子中它是add函数)。 控制器中的代码如下: /** * Returns the image for a function */ @RequestMapping(value = “/function/{functionId}/image.gif”, produces = “image/gif”) public void getImage(@PathVariable(value = “functionId”) String functionId, HttpServletResponse response) throws IOException { Function function = functionService.getFunction(Integer.valueOf(functionId)); Logger logger = Logger.getLogger(FunctionController.class); ServletOutputStream servOut = response.getOutputStream(); // Uses default values if you pass […]

如何在entitymanager createNativeMethod中回滚

我正在使用本机方法的实体管理器,我想在发生一些错误时回滚。为此,我尝试了@Transactional注释,但这不回滚.Below是我的示例代码 调节器 @Autowired ServiceImpl ServiceImpl; @RequestMapping(“/saveinfo”) @ResponseBody @Transactional public String saveinfo(Long id) { ServiceImpl.saveInfo(id); } 服务类 @Autowired DAOImpl daoImpl; @Transactional public String saveinfo(Long id) { daoImpl.saveInfo1(id); daoImpl.saveInfo12(id); daoImpl.saveInfo12(id); } DAOclass @Override public BigInteger saveInfo11() { Query query = entityManagerUtil.entityManager().createNativeQuery(“insert query”); return (BigInteger)query.getSingleResult(); } @Override public BigInteger saveInfo12() { Query query = entityManagerUtil.entityManager().createNativeQuery(“insert query”); return (BigInteger)query.getSingleResult(); […]

使用Powermock测试Spring控制器

我有一个测试特定控制器的类,它工作正常 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = “classpath:config/test-applicationContext-config.xml”) @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class TestAdminController { //….. } 我使用了mockito.org上的Google的Mockito库来模拟我的底层bean。 现在我的问题是:我有一些类需要被嘲笑,但他们有final方法,谷歌的mockito似乎没有解决这个问题。 我的一位同事建议使用powermock.org上的Powermock 。 但它需要使用@RunWith(PowerMockRunner.class)注释来注释测试器类。 如果我使用这个,我必须删除注释@RunWith(SpringJUnit4ClassRunner.class) ,这将给我带来问题,因为不会创建Spring测试上下文。 我怎么能避免这种情况? 建议我配置使用PowerMockRule而不是@RunWith注释 我的项目Maven依赖项如下 org.powermock powermock-mockito-release-full 1.5 pom org.powermock powermock-module-junit4-rule 1.5 test org.powermock powermock-classloading-xstream 1.5 test 现在我的class级看起来像这样(另一个控制器测试) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = “classpath:config/test-applicationContext-config.xml”) @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) @PrepareForTest(ADSynchronizationImpl.class) public class ThirdPartyLoginControllerTest { @Rule public […]

Spring MVC Json解析限制的最佳解决方法

我有一个使用Spring,Hibernate的项目,并且有一个返回JSON的控制器。 当然,我的模型包含使用JPA注释来定义hibernate关系的列表等,因此,例如,我有Users,它们包含一组他们拥有的挑战,同样Challenge包含拥有它的用户。 不幸的是,我似乎在我的JSON中嵌入了很多集合问题。 例如,通过该设置(用户拥有挑战并且挑战拥有所有者),我可以很好地返回挑战。 我可以回复用户就好了。 但是,当我尝试返回一系列挑战时,一切都会爆炸! 我从Jmeter测试中收到以下错误: 错误500服务器错误 我相信这意味着Jackson json解析器在设置json时遇到了问题。 我相信这一点,因为如果我使用@JsonIgnoreProperties({“challengeOwned”}),那么我可以很好地返回挑战列表,因为每个单独的挑战对象不再嵌入其中的列表。 这对我来说似乎很奇怪。 jackson真的不能在JSON中映射简单的嵌入式列表吗? 我也有一个很大的问题,因为我有一个使用User作为其键的Map …而且似乎甚至不可能将JSON map的键定义为嵌入对象! 有没有人对我的问题有任何建议? 我是否必须手动定义一些Json映射? 有一个我不知道的简单解决方案吗? 编辑: 虽然j0ntech所说的确如此,但事实certificate并非整个故事。 似乎当Spring使用Jackson将我的一个hibernate实体序列化为它的JSON版本时,hibernate试图延迟加载该实体的一个属性,但由于该实体在该点处处于其事务之外(在“控制器中”) ),它引起了一个例外,它被吞没了。 实际上有两个问题。 我想通过尝试手动使用Jackson来序列化我实际返回之前返回的对象来解决这个问题。 这样我实际上得到了另一个问题的堆栈跟踪。

如何在Spring 4中使用rapidxml Jackson JSON序列化

当我使用以下curl调用它时,为什么我的Spring控制器不返回媒体类型application/json : curl -v -i -H “Accept: application/json” localhost:8080/properties-ws/prop/2322 我已经阅读了关于这个主题的所有其他问题和答案,没有一个解决我的具体问题。 我正在使用Spring 4,并且有一个简单的Controller和pom。 控制器方法 @RequestMapping(value=”/prop/{character}”, method=RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE}) public @ResponseBody CharData getData(@PathVariable(value=”character”) int codePoint) { CharData chData = null; chData = CharPropertiesService.getProperties(codePoint); return chData; } POM文件依赖jackson 我包括了fastxml jackson-databind工件版本2.3.3 这是pom: 4.0.0 com.joconner.unicode 0.0.1-SNAPSHOT properties-ws properties-ws war 1.7 4.0.3.RELEASE 1.6.9 1.6.1 UTF-8 com.joconner.unicode properties 1.0.0-SNAPSHOT compile com.fasterxml.jackson.core jackson-databind 2.3.3 […]