2017-05-31 53 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