Tag: jersey

在Grizzly上使用JaaS和泽西岛

我正在尝试找到一种简单,灵活的方法来向REST添加JaaS身份validation。 我找到了一篇post ,我认为这个post引导我走向正确的方向(参见StevenC的回答)。 听起来servlet容器负责安全性,而不是Jersey代码本身。 我喜欢这个想法,但需要一点实施指导。 Grizzly是我的servlet容器,我想将其配置为使用JaaS进行身份validation。 现在,一个简单的用户名/密码组合就可以了,直接在代码中硬编码用户名/密码对就可以了。 只要它使用JaaS,我们就可以在以后细化这些细节。 至于通过HTTP发送的内容,我认为存储cookie将是使这一切全部工作的最简单方法。 无论如何使身份validation垃圾远离我的Jersey代码。 以下是到目前为止启动Grizzly的代码: final String baseUri = “http://localhost:9998/”; final Map initParams = new HashMap(); initParams.put(“com.sun.jersey.config.property.packages”, “my.jersey.Service”); System.out.println(“Starting grizzly…”); SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams); System.out.println(String.format( “Jersey app started with WADL available at %sapplication.wadl\n” + “Try out %shelloworld\nHit enter to stop it…”, baseUri, baseUri)); System.in.read(); threadSelector.stopEndpoint(); System.exit(0); 如果整个过程有效,那么检查用户权限的最佳方法是什么? 我可能希望我的REST代码在某些点实际validation权限。 […]

如何使用Dropwizard 1.0.2中的LoggingFeature打印服务器响应?

以下代码导致在Dropwizard 0.9.2和1.0.2中打印JSON服务器响应: return ClientBuilder .newBuilder() .build() .register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true)) 例如: Oct 21, 2016 7:57:42 AM org.glassfish.jersey.filter.LoggingFilter log INFO: 1 * Client response received on thread main 1 < 401 1 < Connection: keep-alive 1 < Content-Length: 49 1 < Content-Type: text/plain 1 < Date: Fri, 21 Oct 2016 07:57:42 GMT 1 < Server: […] 1 […]

泽西岛 – 要求进行身份validation

我在Glassfish上有一个工作的JavaEE 6 Web应用程序,这个应用程序已经有一个JSF前端并且有它的认证机制((使用基于CDI和注释的安全性)所以有一个登录屏幕,用户输入用户名密码,按登录按钮和Java EEvalidation过程开始。 现在我想“也”将我的一些服务类暴露为REST服务(我可能会使用Jersey),因此也可以通过移动设备访问它。 但令我担心的是登录部分。 我将使用完全相同的现有身份validation但现在我希望我的应用程序将从Rest请求获取此凭据,但不从登录屏幕获取。 然后继续使用已存在的现有validation方法(检查来自DB的用户名密码等) 我有点迷路我怎么能这样做,我想我需要使用其中一个filter拦截请求并获取用户名密码但不知道如何以及哪一个? 或者我不需要这样的东西?

Java Jersey Jettison Message Body Readerexception

我是REST服务的完全初学者,但我需要通过REST从网站访问一些信息。 该服务有一些示例代码,以显示如何登录我使用过。 示例代码使用Jettison作为JSON解析器,但是当我尝试运行以下代码片段时,我得到一个exception: JSONObject post = baseResource.path(“login”) .queryParam(“service”, “ABC”).queryParam(“auth”, authParam) .accept(MediaType.APPLICATION_JSON_TYPE).post(JSONObject.class); baseResourse是一个WebResource对象。 代码失败,出现以下exception: Exception in thread “main” com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type application/json; character=utf-8 was not found 示例代码并不表示我应该添加任何“消息体阅读器”来处理响应? 或者我是否需要添加或执行任何明显的解析响应? 谢谢。

在Jackson / Jaxb中打开一个元素

我使用Jersey + Jackon制作一个可以与JSON一起使用的REST API。 假设我有一个如下课程: @XmlRootElement public class A { public String s; } 这是我使用该类的泽西方法: @GET @Produces(MediaType.APPLICATION_JSON) public Object get(@PathParam(“id”) String id) throws Exception{ A[] a= new A[2]; a[0] = new A(); a[0].s=”abc”; a[1] = new A(); a[1].s=”def”; return a; } 出局是: {“a”:[{“s”:”abc”},{“s”:”def”}]} 但我希望它是这样的: [{“s”:”abc”},{“s”:”def”}] 我该怎么办? 请帮帮我。

使用Jersey 2.21在REST API请求中的可选参数

我正在玩Jersey 2.21 ,我想知道是否可以有一个“可选”的参数,它可以或不存在于对服务器的请求中。 我想成功访问这两种方法: http://localhost:8080/my_domain/rest/api/myMethod/1 http://localhost:8080/my_domain/rest/api/myMethod 正如您所看到的,我正在尝试使整数( id )参数成为可选参数。 我已经将myMethod声明如下: @GET @Path(“myMethod/{id}”) @Produces(MediaType.APPLICATION_JSON + “;charset=UTF-8”) public String myMethod(@PathParam(“id”) Integer id, @Context HttpHeaders hh) 这有效: http://localhost:8080/my_domain/rest/api/myMethod/1 这也有效: http://localhost:8080/my_domain/rest/api/myMethod/ 但这不起作用 ,我不明白为什么。 它抛出404 Not Found错误: http://localhost:8080/my_domain/rest/api/myMethod 你能指出我正确的方向来解决这个问题吗? 我不喜欢斜线在我所有的REST方法调用中都是强制性的,并且如果可能的话想要压缩它。

如何使用泽西拦截器来获取请求体

我在我的项目中使用REST-Jersey 。 所有POST数据都以JSON格式发送,并在服务器端解组为相应的bean。 像这样的东西: 向服务器发送请求: $(‘a#sayHelloPost’).click(function(event){ event.preventDefault(); var mangaData = { title:’Bleach’, author:’Kubo Tite’ } var formData=JSON.stringify(mangaData); console.log(formData); $.ajax({ url:’rest/cred/sayposthello’, type: ‘POST’, data: formData, dataType: ‘json’, contentType:’application/json’ }) }); 有效载荷: {“title”:”Bleach”,”author”:”Kubo Tite”} 服务器端: @POST @Path(“/sayposthello”) @Produces(MediaType.APPLICATION_JSON) public Response sayPostHello(MangaBean mb){ System.out.println(mb); return Response.status(200).build(); } MangaBean: public class MangaBean { private String title; private String author; […]

运行Jersey项目(Rest Web服务)到tomcat

我试着遵循本指南: 教程,但是当我在Web浏览器上执行它时会返回错误: HTTP Status 404 – / type Status report message / description The requested resource (/rest.api/rest/hello) is not available. 我在我的mac上的/ usr / local中安装了tomcat7,并在终端上使用startup.sh运行它。 这是我的web.xml和Hello.java HelloRest Jersey REST Service com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages rest.api 1 Jersey REST Service /rest/* package rest.api; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; // POJO, no interface no extends // The […]

不知道为什么:ResourceConfig实例不包含任何根资源类

我是jersey和网络服务的新手,我尝试运行一个简单的RESTful Web服务。 我关注了http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/,但我的项目没有使用maven,我下载了jersey.1.17.1.jar并将其包含在我的项目路径。 当我想在http://localhost:8080/sycotext/rest/service/SOMETEXT上调用服务时出现此错误: HTTP Status 500 – Servlet.init() for servlet sycoText-servlet threw exception 这是堆栈跟踪: javax.servlet.ServletException: Servlet.init() for servlet sycoText-servlet threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1010) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640) org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1618) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1576) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) java.lang.Thread.run(Thread.java:724) root cause com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. com.sun.jersey.server.impl.application.RootResourceUriRules.(RootResourceUriRules.java:99) com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1331) com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774) com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770) com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770) com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765) […]

与Jersey和JSR相关的JAX-RS

我试图了解Java中的一些概念: JSR:描述规范,但没有实际实现。 例如, http://jsr311.java.net/是“用于RESTful Web服务的Java™API”的“主页”。 它是JSR-311所有实现的通用参考。 可以从http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api下载JSR-311的接口(?),但是,除非您自己实现JSR-311,否则它们没有特别的价值? JSR通常/总是有一个参考实现。 要找到它,你必须google“JSR XXX参考实现”或查看规范主页(例如http://jsr311.java.net/ ) 对于JSR-311,这个参考实现是Jersey 。 使用maven,您可以从http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9获取jersey服务器。 由于Jersey根据http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api中的接口提供了一个实现,因此您只需要在项目中添加Jersey作为依赖项,而不是jsr311-api本身。 (这适用于所有JSR技术?) 将http://mvnrepository.com/artifact/javax.ws.rs/jsr311-api和http://mvnrepository.com/artifact/com.sun.jersey/jersey-server/1.9作为项目中的依赖项放在一起导致类路径问题? 我是完全不喜欢的吗?