Tag: jersey

Jersey RESTful Web服务gradle设置

我坚持使用jersey库为RESTful Web服务创建一个gradle项目。 项目配置应该能够在jetty应用程序服务器中启动服务。 我已经找到了一个资源: https : //github.com/ziroby/jetty-gradle-hello-world 我的解决方案的问题是,它使用了过时的jersey版本。 我至少需要2版(最新的2.14版)。 我试图在maven central上搜索新版本,但在版本2中,很多工件名称发生了变化,我无法正确配置它。 编辑:我的项目中并不特别需要Jetty服务器。 它可以是任何应用程序服务器,适用于测试和调试我的应用程序。 我也在生产中使用docker,所以使用docker会很不错。 编辑 :(通过peeskillet) – 链接代码 建立 apply plugin: ‘java’ apply plugin: ‘jetty’ repositories { mavenCentral() } dependencies { testCompile ‘junit:junit:4.11’ testCompile ‘org.hamcrest:hamcrest-all:1.3’ testCompile ‘com.sun.jersey:jersey-client:1.17.1’ testCompile ‘com.sun.jersey:jersey-core:1.17.1’ compile ‘com.sun.jersey:jersey-core:1.17.1’ compile ‘com.sun.jersey:jersey-server:1.17.1’ compile ‘com.sun.jersey:jersey-servlet:1.17.1’ } test { exclude ‘**/*IntegrationTest*’ } task integrationTest(type: Test) { […]

@Path和正则表达式(Jersey / REST)

我在REST项目中使用Jersey ,我需要使用正则表达式 。 挖掘它很简单,如: @Path(“/resources”) public class MyResource { @GET @Path(“{subResources:.*}”) public String get(@PathParam(“subResources”) String subResources) {…} } 但是,这样做, 只有当我传递1个参数时,该方法才会获取请求,例如: GET:… / resources / firstSubResource 如果我使用多于1个参数,则该方法无法获取请求,例如: GET:… / resources / firstSubResource / seccondSubResource / thirdSubResource 如果我的@Path包含变量或文本值 ,我只能使用正则表达式,例如: @Path(“{SubResource1}/{subResources:.*}”) 要么 @Path(“/hardCodeString/{subResources:.*}”) 今天我可以使用这个变量的解决方案来运行,但对于我的观点来说并不是oK。 我的web.xml (…) Jersey Spring Web Application com.sun.jersey.spi.spring.container.servlet.SpringServlet com.sun.jersey.config.property.packages com.myproject.rest 1 Jersey Spring Web Application […]

例外:com.sun.jersey.spi.inject.Errors $ ErrorMessagesException

我正在使用Jersey API进行Web服务。 我正在从客户端向服务器发送多部分数据。 Web服务开始执行时,我遇到exception。 @POST @Path(“uploadphoto”) @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(“text/plain”) public String uploadNotices(@FormDataParam(“file”) InputStream uploadedInputStream, @FormDataParam(“file”) FormDataContentDisposition fileDetail) { String uploadedFileLocation = “d:/” + fileDetail.getFileName(); // save it try { writeToFile(uploadedInputStream, uploadedFileLocation); } catch(Exception e) { return “no”; } return “yes”; } // save uploaded file to new location private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) throws Exception […]

启用AngularJS到Jersey的CORS发布请求

我正在尝试将一个JSON文档从AngularJS应用程序发布到Jersey REST服务。 请求失败,通知我: XMLHttpRequest cannot load http://localhost:8080/my.rest.service/api/order/addOrder. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost’ is therefore not allowed access. Jersey REST Postfunction 我启用了(我相信的)适当的标题:响应的Access-Control-Allow-Origin和Access-Control-Allow-Methods ,如下面的方法所示: @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path(“/addOrder”) public Response addOrder(DBObject dbobject) { DB db = mongo.getDB(“staffing”); DBCollection col = db.getCollection(“orders”); col.insert(dbobject); ObjectId id = (ObjectId)dbobject.get(“_id”); return Response.ok() .entity(id) .header(“Access-Control-Allow-Origin”,”*”) .header(“Access-Control-Allow-Methods”, […]

找不到Jersey + Json媒体类型应用程序/ json

我正在尝试简单的Jersey + JSON示例,但我得到以下错误message body writer for Java class com.test.jsonexample and MIME media type application/json was not found 我把以下jar文件用于获得适当的结果 asm-3.1.jar jackson-core-asl-1.9.9.jar jackson-jaxrs-1.9.9.jar jackson-mapper-asl-1.9.9.jar jackson-xc-1.9.9.jar jersey-client-1.9.jar jersey-core-1.9.1.jar jersey-json-1.9.jar jersey-server-1.9.1.jar jettison-1.3.2.jar jsr311-api-1.1.1.jar 为什么我会收到此类错误? 错误日志在这里: SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.test.Jsonexample, and Java type class com.test.Jsonexample, […]

Java HTTP代理服务器

我需要实现一个HTTP代理服务器应用程序,它将来自多个客户端的请求代理到远程服务器。 以下是步骤: 客户转发请求代理 代理转发请求到服务器 服务器向Proxy返回请求 代理向客户返回请求。 我只是不确定如何实现这个代理。 我的第一个想法是实现一个tomcat应用程序,它使用jersey / apache httpclient将请求转发给远程服务器并将响应返回给客户端? 有没有更好的方法来实现这样的代理服务器? 代理需要处理多个线程。

只有当依赖性组装成一个jar子时才会抛出泽西exception

我正在编写一个嵌入Jetty w / Jersey的服务器。 当我从Eclipse执行时,一切都很棒。 但是,如果我使用Maven的程序集将我的服务器和所有依赖项组装到一个jar中:单个目标,我会得到一个例外: Sep 26, 2012 5:35:59 PM com.sun.jersey.spi.container.ContainerResponse write SEVERE: A message body writer for Java class com.acme.server.webservice. exception.WebServiceFailure, and Java type class com.acme.server.webserv ice.exception.WebServiceFailure, and MIME media type application/json was not fo und Sep 26, 2012 5:35:59 PM com.sun.jersey.spi.container.ContainerResponse write SEVERE: The registered message body writers compatible with the MIME […]

泽西客户:向请求添加Cookie

我正在尝试编写一个使用Jersey Client API访问RESTful Web服务的库。 该服务需要一个设置cookie的登录请求,然后后续请求必须将该cookie设置为成功。 登录请求按预期工作,我能够从登录中检索响应中的cookie,但似乎无法在后续请求中添加cookie。 任何人都可以告诉我可能做错了什么。 以下是发出请求的代码: MultivaluedMap qs = new MultivaluedMapImpl(); qs.add( “xml”, this.toXmlString() ); WebResource wr = client.resource( Constants.ServiceURL ); if ( CookieJar.Cookies != null ) { for ( NewCookie c : CookieJar.Cookies ) { logger.debug( “Setting cookie ” + c.getName() ); wr.cookie( c ); } } ClientResponse response = wr.queryParams( qs […]

在JAX-RS中使用@ Context,@ Provider和ContextResolver

我刚刚熟悉使用JAX-RS在Java中实现REST Web服务,我遇到了以下问题。 我的一个资源类需要访问存储后端,后端在StorageEngine接口后面被抽象出来。 我想将当前的StorageEngine实例注入到为REST请求提供服务的资源类中,我认为这样做的好方法是使用@Context注释和适当的@Context类。 这是我到目前为止: 在MyResource.java : class MyResource { @Context StorageEngine storage; […] } 在StorageEngineProvider.java : @Provider class StorageEngineProvider implements ContextResolver { private StorageEngine storage = new InMemoryStorageEngine(); public StorageEngine getContext(Class type) { if (type.equals(StorageEngine.class)) return storage; return null; } } 我正在使用com.sun.jersey.api.core.PackagesResourceConfig自动发现提供程序和资源类,并根据日志,它很好地获取StorageEngineProvider类(故意留下时间戳和不必要的东西): INFO: Root resource classes found: class MyResource INFO: Provider classes found: class […]

DropWizard Auth by Example

我试图了解DropWizard中的身份validation和授权是如何工作的。 我已经阅读了他们的auth指南以及GitHub上的dropwizard-security项目,但我觉得我仍然缺少一些重要的概念。 public class SimpleCredential { private String password; public SimpleCredential(String password) { super(); this.password = password; } } public class SimplePrincipal { pivate String username; public SimplePrincipal(String username) { super(); this.username = username; } } public class SimpleAuthenticator implements Authenticator { @Override public Optional authenticate(SimpleCredential credential) throws AuthenticationException { if(!”12345″.equals(credential.getPassword())) { throw new AuthenticationException(“Sign […]