java – 如何使用Grizzly2在Jersey中以编程方式启用POJO映射?

前端之家收集整理的这篇文章主要介绍了java – 如何使用Grizzly2在Jersey中以编程方式启用POJO映射?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
按照 here的说明我有这个代码
  1. private static URI getBaseURI() {
  2. return UriBuilder.fromUri("http://localhost/").port(9998).build();
  3. }
  4.  
  5. public static final URI BASE_URI = getBaseURI();
  6.  
  7. protected static HttpServer startServer() throws IOException {
  8. System.out.println("Starting grizzly...");
  9. final ResourceConfig rc = new PackagesResourceConfig("amplify.api.resources");
  10. return GrizzlyServerFactory.createHttpServer(BASE_URI,rc);
  11. }
  12.  
  13. public static void main(final String[] args) throws IOException {
  14. final HttpServer httpServer = startServer();
  15. System.out.println(String.format("Jersey app started with WADL available at "
  16. + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",BASE_URI,BASE_URI));
  17. System.in.read();
  18. httpServer.stop();
  19. }

接下来我想启用here所述的JSON POJO支持,但问题是我想以编程方式而不是通过web.xml文件(我没有web.xml文件!)来实现.

如何修改上面的代码以启用JSON POJO映射功能

解决方法

  1. protected static HttpServer startServer() throws IOException {
  2. System.out.println("Starting grizzly...");
  3. final ResourceConfig rc = new PackagesResourceConfig("amplify.api.resources");
  4. rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,true);
  5. return GrizzlyServerFactory.createHttpServer(BASE_URI,rc);
  6. }

猜你在找的Java相关文章