我在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);
}
}
爲什麼你不使用@RequestMapping的REST調用地圖嗎? –
你可以顯示映射'category/list/{id}'路徑的控制器片段嗎? – Andremoniy
在我的RESTful服務的代碼示例中添加了上面的UPDATE。 – Richard