我在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