I am in the wall on how to set up my application. The goal is to have the following on a single Tomcat server:
- Full service (for example, a hyperlink layer that returns the application format / hal + json)
- Leisure service - old fashion (access to the controller)
- Classic webpage delivery controller set
Wednesday:
<spring-framework.version>4.0.1.RELEASE</spring-framework.version>
<spring-framework.version>4.0.1.RELEASE</spring-framework.version>
<spring-test.version>4.0.1.RELEASE</spring-test.version>
<spring-data-rest-webmvc.version>2.0.0.RC1</spring-data-rest-webmvc.version>
<spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version>
<spring-data-commons.version>1.7.0.RC1</spring-data-commons.version>
Repository:
@RepositoryRestResource( path = "u")
public interface IUserRepository extends CrudRepository<Users, Long> {
Users findByName(@Param("name") String name);
}
Stop Controller:
@RestController
@RequestMapping (value = "/users", produces=MediaType.APPLICATION_JSON_VALUE)
public class RestUserController {
@Autowired
private IService service;
@RequestMapping (method = RequestMethod.GET)
public @ResponseBody Iterable<DomainModel.User> findAllUsers() {
return service.findAllUsers();
}
}
The web controller:
@Controller
@RequestMapping (value = "/web")
public class HomeController {
@RequestMapping(method = RequestMethod.GET, value = "/home")
public String displayHome(Model model) {
return "home"
}
JPA configuration (for simplicity, I just put an ad for the jpa data source ...)
@Configuration
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan(basePackages = { "com.xxx.controller.rest", com.xxx.services.rest.impl" })
@EnableJpaRepositories(basePackages = "com.xxx.persistence.repository")
@EnableTransactionManagement
public class ConfigForJpa {
...
}
Configuration for WebMvc:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.xxx.controller.web" })
public class ConfigForWebMvc extends WebMvcConfigurerAdapter {
private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views";
private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
...
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);
viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);
return viewResolver;
}
...
}
Configuration for repository:
@Configuration
@ComponentScan(basePackages = { "com.xxx.controller.rest" })
public class ConfigForRepositoryRestMvc extends RepositoryRestMvcConfiguration {
}
Initializer Configuration:
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { ConfigForJpa.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { ConfigForWebMvc.class,
ConfigForRepositoryRestMvc.class };
}
@Override
protected String getServletName() {
return " ServletName";
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Remember that the root context of the network is set to "sr"
When the tomcat server is running, I can clearly see which mappings are occurring.
RequestMappingHandlerMapping - Mapped "{[/web/home],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.xxx.controller.web.HomeController.displayHome
RequestMappingHandlerMapping - Mapped "{[/users/user],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public com.xxx.model.DomainModel$User com.xxx.controller.rest.RestUserController
...
SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
...
RepositoryRestHandlerMapping - Mapped "{[/{repository}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.hateoas.Resources
RepositoryRestHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.data.rest.webmvc.RepositoryLinksResource org.springframework.data.rest.webmvc.RepositoryController.listRepositories()
Good:
xlocalhostx: 8080/sr (sr - -), :
{ "_links": { "": { "href": "http://xlocalhostx:8080/sr/u" } }
}
xlocalhostx:8080/sr/users, :
[{ "ID": 1, "": "", "": "" }, { "ID": 2, "": "Lois", "": "" }]
, :
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { ConfigForWebMvc.class, ConfigForRepositoryRestMvc.class} )
public class HomeControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getHome() throws Exception {
this.mockMvc.perform(get("/web/home"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/views/home.jsp"));
}
}
- xlocalhostx: 8080/sr/ web/home 404, , . (my home.jsp, , WEB-INF views. , HomeController , .
, , :
a 404:/sr/WEB-INF/viewshome.jsp .
, , - ( "sr" ) home.jsp.
.
.