2017-05-31 36 views
1

我在Spring 4.3.1.RELEASE中使用Java8。我有一个项目需要同时提供静态html页面和RESTful服务。带有REST风格的服务和静态HTML的Spring MVC

我可以一次在一个工作,但不能同时工作。

例如,我需要访问:

http://localhost:8080/jbosswildfly-1.0/category/list 
http://localhost:8080/jbosswildfly-1.0/category/list/{id} 

http://localhost:8080/jbosswildfly-1.0/index.html 
http://localhost:8080/jbosswildfly-1.0/tocs.html 
http://localhost:8080/jbosswildfly-1.0/www/index.html 

我的问题是关于我的servlet-mapping

我有以下几点:

public class WebAppInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(AppConfig.class); 
     ctx.setServletContext(servletContext); 
     Dynamic rest = servletContext.addServlet("rest", new DispatcherServlet(ctx)); 
     //dynamic.addMapping("/**/*.do"); 
     rest.addMapping("/*.html"); 
     rest.addMapping("/category/list"); 
     rest.addMapping("/category/list/*"); 
     rest.setLoadOnStartup(1); 
    } 
} 

我已经尝试了数映射的组合,但似乎无法得到静态内容和RESTful服务同时工作。

在上面的例子中,我可以得到以下工作:

http://localhost:8080/jbosswildfly-1.0/index.html 
http://localhost:8080/jbosswildfly-1.0/snoop.jsp 
http://localhost:8080/jbosswildfly-1.0/WebContent/index.html 
http://localhost:8080/jbosswildfly-1.0/category/list 

但是,以下未发现:

http://localhost:8080/jbosswildfly-1.0/category/list/AC 

下允许访问的RESTful服务,但不是静态html文件:

rest.addMapping("/"); 

任何帮助表示赞赏。

UPDATE

这里是我的RESTful服务的一个代码:

@CrossOrigin(origins = {"*"}) 
@RestController 
@RequestMapping(CategoryRESTService.BASE_URI) 
public class CategoryRESTService { 

    public static final String BASE_URI = "/category"; 

    @Autowired 
    private CategoryService categoryService; 

    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<List<Category>> findAllCategorys() { 
     List<Category> categories = categoryService.findAll(); 
     return new ResponseEntity<List<Category>>(categories, HttpStatus.OK); 
    } 

    @RequestMapping(value = "/list/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<Category> findCategoryById(@PathVariable String id) { 
     Category category = categoryService.findById(id); 
     return new ResponseEntity<Category>(category, HttpStatus.OK); 
    } 
} 
+0

为什么你不使用@RequestMapping的REST调用地图吗? –

+0

你可以显示映射'category/list/{id}'路径的控制器片段吗? – Andremoniy

+0

在我的RESTful服务的代码示例中添加了上面的UPDATE。 – Richard

回答

1

尝试使用rest.addMapping("/");映射,你必须配置静态资源解析器的同时,例如

通过xml配置

<mvc:resources mapping="*.html" location="location of the resource folder" /> 

或Java的基础配置

@Configuration 
@EnableWebMvc 
public class MvcConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry 
      .addResourceHandler("*.html") 
      .addResourceLocations("location of the resource folder"); 
    } 
} 
+0

嗨美拉德,感谢您的反馈。我尝试以下,但它不起作用(即没有找到'index.html')。也许我的资源位置不正确? 'registry.addResourceHandler( “* HTML”)addResourceLocations( “/ web应用/”);'。我的'index.html'是'src/main/webapp/index.html'。 – Richard

+0

固定,我加了这个,它发现'index.html':'addResourceLocations(“/”);'。非常感谢你。 – Richard

+0

'addResourceLocations(“/”)'适用于'webapp'目录。但是,如果我有另一个带有html文件的目录,即'/ webapp/www/index.html',则添加另一个'addResourceLocations(“/ www /”)'不起作用。 – Richard