I have a problem integrating this two technologies.
We have an GWT application integrated with Spring but, additionally to GWT application we need to display HTML files using Thymeleaf for special modules.
The GWT application works well but when I try to run it with Controllers and Thymeleaf enabled this fail.
I've added this to my web.xml to support the Spring Controllers use:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring/controller-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And my controller-context.xml has this beans:
<bean id="uiengineTemplateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/uiengine/target/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<array>
<ref bean="uiengineTemplateResolver"/>
</array>
</property>
</bean>
And this at the end:
<context:component-scan base-package="com.enterprise.platform.*.gui" />
There is a way to integrate this two frameworks?
I'll appreciate your help!
Thanks.
I've resolved!
First you need to exclude the main HTML file for GWT from ThymeleafViewResolver so:
controller-context.xml
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="excludedViewNames">
<array>
<value>gwt-project.html</value>
</array>
</property>
</bean>
Allow the static access for the resources of GWT application:
<mvc:resources mapping="/gwt-project/**" location="/gwt-project/" />
Create a Spring Controller with access root method:
MyController.java
#Controller
public class MyController
{
#RequestMapping(value = "/{appBeanId}", method = RequestMethod.GET)
public String root(#PathVariable String appBeanId, Locale locale,
org.springframework.ui.Model model) {
....
}
}
That's it!.
Related
Versions (SpringBoot is not involved):
Spring: 5.2.16
web-app / servlet API: 4.0
JUnit: 5.8
Spring MVC Testing is not working for controller endpoint that returns ResponseEntity<ReturnStatus>, where ReturnStatus is a POJO with appropriate getters/setters. The exception triggered indicates that JSON conversion is not working for ReturnStatus. My research indicates that the annotation-based Java configuration for the WebApplicationContext is not loaded (and therefore the Jackson JSON converter is not recognized). Curiously, in a non-testing deployment in Tomcat, the controller endpoint works fine, presumably because the web.xml in the war-file is parsed by Tomcat.
QUESTION:
How can I adjust the setup for Spring MVC Test for this application so that the annotation-based Java configuration for the WebApplicationContext is properly loaded? Can this, for example, be done explicitly in the endpoint-test logic (ie, the JUnit test)?
Exception:
14:33:57,765 WARN DefaultHandlerExceptionResolver:199 - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.acme.myapp.io.ReturnStatus] with preset Content-Type 'null']
14:33:57,765 DEBUG TestDispatcherServlet:1131 - Completed 500 INTERNAL_SERVER_ERROR
The Spring MVC app incorporates the following configurations:
test-context.xml, which houses Spring bean-configuration for access to data store:
web.xml, which declares and maps the DispatcherServlet with relevant setup for WebApplicationContext.
Annotation-based configuration in Java implementation of WebMvcConfigurer.
Relevant excerpt from test-context.xml:
<context:component-scan base-package="com.acme.myapp"/>
<jpa:repositories base-package="com.acme.myapp.repos"/>
<context:property-placeholder location="classpath:/application.properties" />
<!-- Data persistence configuration -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${db.showSql}" />
<property name="databasePlatform" value="${db.dialect}" />
<property name="generateDdl" value="${db.generateDdl}" />
</bean>
</property>
<property name="packagesToScan">
<list>
<value>com.acme.myapp.dao</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.pass}" />
<property name="initialSize" value="2" />
<property name="maxActive" value="5" />
<property name="accessToUnderlyingConnectionAllowed" value="true"/>
</bean>
<!-- Set JVM system properties here. We do this principally for hibernate logging. -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="getProperties" />
</bean>
</property>
<property name="targetMethod" value="putAll" />
<property name="arguments">
<util:properties>
<prop key="org.jboss.logging.provider">slf4j</prop>
</util:properties>
</property>
</bean>
Relevant excerpt from web.xml (where application-context.xml is our production version of test-context.xml):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>central-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.myapp.MyAppWebAppConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>central-dispatcher</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Excerpt from Java implementation of WebMvcConfigurer (ie, where we incorporate Jackson JSON converter):
#EnableWebMvc
#Configuration
#ComponentScan(basePackages = { "com.acme.myapp.controllers" })
public class MyAppWebAppConfig implements WebMvcConfigurer
{
private static final Logger logger = LoggerFactory.getLogger(MyAppWebAppConfig.class);
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters)
{
logger.debug("extendMessageConverters ...");
converters.add(new StringHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter(new MyAppObjectMapper()));
}
}
The controller endpoint looks like this (where the root is at /patients):
#RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ReturnStatus> readPatient(
#PathVariable("id") long id
)
{
ReturnStatus returnStatus = new ReturnStatus();
returnStatus.setVersionId("1.0");
...
return new ResponseEntity<ReturnStatus>(returnStatus, httpStatus);
}
Using JUnit5 and MockMvc, the endpoint-test looks like this:
#SpringJUnitWebConfig(locations={"classpath:test-context.xml"})
public class PatientControllerTest
{
private MockMvc mockMvc;
#BeforeEach
public void setup(WebApplicationContext wac) {
this.mockMvc = webAppContextSetup(wac).build();
}
#Test
#DisplayName("Read Patient from /patients API.")
public void testReadPatient()
{
try {
mockMvc.perform(get("/patients/1").accept(MediaType.APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Thanks!
Here are some options, possibly not exhaustive:
Per earlier comment, we can simply use <mvc:annotation-driven> directive in test-context.xml. For example:
<bean id="myappObjectMapper" class="com.acme.myapp.MyAppObjectMapper"/>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<constructor-arg ref="myappObjectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Effectively, this directive obviates the need for loading MyAppWebAppConfig, as <mvc:annotation-driven> in fact is the XML-equivalent of the annotation #EnableWebMvc in Java.
Implement WebApplicationInitializer so that effectively does in Java what we configure into web.xml. For example:
public class MyAppWebApplicationInitializer implements WebApplicationInitializer
{
#Override
public void onStartup(ServletContext container)
{
XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
appCtx.setConfigLocation("classpath:application-context.xml");
container.addListener(new ContextLoaderListener(appCtx));
AnnotationConfigWebApplicationContext dispatcherCtx = new AnnotationConfigWebApplicationContext();
dispatcherCtx.register(MyAppWebAppConfig.class);
ServletRegistration.Dynamic registration = container.addServlet("central-dispatcher", new DispatcherServlet(dispatcherCtx));
registration.setLoadOnStartup(1);
registration.addMapping("/api/*");
}
}
For this solution, we expunge web.xml from the project; possibly we should parameterize the reference to application-context.xml as well.
Note that when I run JUnit5 tests, it appears that Spring does not instance MyAppWebApplicationInitializer, and that instead, the Spring context loaded for JUnit5 is the one referenced by the #SpringJUnitWebConfig annotation. I therefore recommend co-locating test-related configuration with test-context.xml, and reserving WebApplicationInitializer for production.
I'm sure there are other options, but I've only explored these two approaches.
This is my web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/app-root.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
This is my springmvc-servlet.xml
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.***.***">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
This is my app-root.xml
<import resource="classpath:spring/app-dao.xml"/>
<import resource="classpath:spring/shiro.xml"/>
<import resource="classpath:spring/app-timer.xml"/>
<import resource="classpath:spring/app-context.xml"/>
This is my app-context.xml
<context:component-scan base-package="com.xinshen.meeting">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
This is my app-datasource.xml
<bean id="adminTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="adminTxManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
<!--扫描entity包,使用别名-->
<property name="typeAliasesPackage" value="com.xinshen.meeting.model"/>
<property name="mapperLocations" value="classpath*:mappers/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.***.***.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
when I add #Transactional to Controller ,transaction it works but Service it does`t work!
the following pic is #Transactional on Service
enter image description here
the following pic is #Transactional on Controller
enter image description here
I really can`t finger it out,Thanks for help!
I think You have to enable transaction using configuration add this annotation #EnableTransactionManagement in a config class
Create a class for configuration :
#Configuration
#ComponentScan(basePackages = { "Your services package or base package" })
#EnableTransactionManagement
public class MyConfig {
}
If your service are not implementing interface, you need to set proxy-target-class="true" in tag tx:annotation-driven.
You also should add #Transactional on the service class, or on the function invoked by spring bean directly.
It's My Project
enter image description here
It's My spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.SpringDemo.Controller"/>
<!-- Thymeleaf Template Resolver -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="order" value="1" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="enableSpringELCompiler" value="true" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="viewNames" value="thymeleaf/*"/>
<property name="templateEngine" ref="templateEngine" />
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="" />
<!--<property name="suffix" value=".jsp" />-->
<property name="order" value="2" />
<property name="viewNames" value="*.jsp" />
</bean>
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
It's My Controller
#Controller
public class HomeController{
//It's OK
#RequestMapping("/home")
public String showHomePage(Model model){
model.addAttribute("name","spring-mvc");
return "thymeleaf/testTh";
}
//It's WRONG
#RequestMapping("/index")
public ModelAndView test(ModelAndView mv){
mv.addObject("name","erer");
mv.setViewName("index.jsp");
return mv;
}
}
It's My Exception
enter image description here
In's My Web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 加载springMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I want that all requests are first passed through the controller.
And then return to the page
Please import spring-boot-starter-thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
I think the problems may be located on the returned view name. Since the view name is the same as the /index path, so maybe it will be dispatched by spring dispatcher again. Maybe you can try to change the view as another name to have a try.
My Spring JSP is not coming up when I call it. Apparently the resource is not available. Here is my controller code.
#Controller
public class BulletinsController {
private List<Bulletin> bulletins;
private BulletinDAO bulletinDAO;
// getters and setters, including autowiring for the BulletinDAO
#RequestMapping(value = "/getApprovedBulletins", method = RequestMethod.POST)
public ModelAndView getApprovedBulletins() {
try {
bulletins = bulletinDAO.getApprovedBulletins();
mav.setViewName("WEB-INF/jsp/EnterBulletin");
if (bulletins != null) {
mav.addObject("bulletins", bulletins);
}
} catch (Exception e) {
System.out.println(e.getMessage());
mav.setViewName("WEB-INF/jsp/FailurePage");
}
return mav;
}
Here is the relevant part of jcbulboard-servlet.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/cpc" />
</bean>
<bean id="bulletinDAO" class="com.dao.BulletinDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="bulletinsController" class="com.controller.BulletinsController">
<property name="bulletinDAO" ref="bulletinDAO" />
</bean>
Here is my web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Job Connections Bulletin Board</display-name>
<servlet>
<servlet-name>jcbulboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jcbulboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Try adding a view resolver to your jcbulboard-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Instead of
mav.setViewName("WEB-INF/jsp/FailurePage");
It would be
mav.setViewName("FailurePage");
Assuming there is a file named:
WEB-INF/jsp/FailurePage.jsp
I am having a trouble mapping a specific URL request to one of the controllers in my project.
the URL is : http://HOSTNAME/api/v1/profiles.json
the war which is deployed is: api.war
the error I get is the following:
[PageNotFound] No mapping found for
HTTP request with URI
[/api/v1/profiles.json] in
DispatcherServlet with name 'action'
The configuration I have is the following: web.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-security.xml</param-value>
</context-param>
<!-- Cache Control filter -->
<filter>
<filter-name>cacheControlFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<!-- Cache Control filter mapping -->
<filter-mapping>
<filter-name>cacheControlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<!-- Spring security filter mapping -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Controller -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
The action-servlet.xml:
<mvc:annotation-driven/>
<bean id="contentNegotiatingViewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="true" />
<!--
default media format parameter name is 'format'
-->
<property name="ignoreAcceptHeader" value="false" />
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</bean>
</list>
</property>
</bean>
the application context security:
<sec:http auto-config='true' >
<sec:intercept-url pattern="/login.*" filters="none"/>
<sec:intercept-url pattern="/oauth/**" access="ROLE_USER" />
<sec:intercept-url pattern="/v1/**" access="ROLE_USER" />
<sec:intercept-url pattern="/request_token_authorized.jsp" access="ROLE_USER" />
<sec:intercept-url pattern="/**" access="ROLE_USER"/>
<sec:form-login authentication-failure-url ="/login.html"
default-target-url ="/login.html"
login-page ="/login.html"
login-processing-url ="/login.html" />
<sec:logout logout-success-url="/index.html" logout-url="/logout.html" />
</sec:http>
the controller:
#Controller
public class ProfilesController {
#RequestMapping(value = {"/v1/profiles"}, method = {RequestMethod.GET,RequestMethod.POST})
public void getProfilesList(#ModelAttribute("response") Response response) {
....
}
}
the request never reaches this controller.
Any ideas?
Annotations don't do anything until they are processed. You will need
<context:component-scan base-package="path.to.controllers"/>
as a child of your root "beans" tag in order to get Spring to scan for controllers. Spring will scan for controllers in the base-package and it's descendants.
This tags requires
xmlns:context="http://www.springframework.org/schema/context"
and
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
in your root "beans" tag.