Tag: jersey

公共和私人资源使用不同的路径Jersey + Spring启动

我正在使用Spring boot + Jersey + Spring安全性,我想拥有公共和私有端点,我想要一个架构如下: / rest – 我的根上下文 / public – 我想将我的公共端点放在这个上下文中,它必须在/rest/public/pings类的根上下文中 / private – 我想将我的私有端点放在这个上下文中,它必须在/rest/private/accounts类的根上下文中 我的配置如下: 泽西配置: @Configuration @ApplicationPath(“/rest”) public class RestConfig extends ResourceConfig { public RestConfig() { register(SampleResource.class); } } Spring安全配置: @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { …….. protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(“/rest/public/**”).permitAll(); http.antMatcher(“/rest/**”).authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic(); http.csrf().disable(); } } […]

在Jersey中使用名称绑定注释

@NameBinding注释如何在Jersey中对特定资源方法或资源类应用filter? 请考虑以下注释: @NameBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface SomeAnnotaion{} 它是如何工作的?

将List / Map作为POST参数泽西发送

我想将HashMap对象作为POST变量发送到ReST资源。 我使用Form类发送对象。 客户端代码: public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); HashMap hashmap = new HashMap(); hashmap.put(“Key1”, “value1”); hashmap.put(“Key2”, “value2”); Form form = new Form(); form.add(“data1”, hashmap); ClientResponse response = service.path(“hello2”).path(“hello2”).type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form); @SuppressWarnings(“unchecked”) MultivaluedMap map = response.getEntity(MultivaluedMap.class); System.out.println(map.get(“response”).get(0)); System.out.println(map.get(“response2”)); } REST资源如下: @Path(“/hello2”) public class […]

如何使用Moxy和Jersey从HashMap返回JSON对象

我使用Jersey 2.17和Moxy,我有以下function: @Produces(APPLICATION_JSON) @Restricted public List getFriends( @PathParam(“user”) String user ) { return userDAO.getFriends(user); } User.preferences是一个HashMap 。 它适用于几乎所有对象,除了HashMap ,它被翻译成: “偏好”:{ “条目”:[{ “键”:{ “类型”: “串”, “值”: “语言”}, “值”:{ “类型”: “串”, “值”: “恩”}},{ “关键”:{ “类型”: “串”, “值”: “国”}, “值”:{ “类型”: “串”, “值”: “US”} }]} 但我真的想要返回的只是一个javascript对象,如: 喜好:{ “语言”: “恩”, “国”: “US”} 我怎样才能做到这一点?

如何使用Jersey REST序列化Java基元

在我的应用程序中,我使用Jersey REST来序列化复杂对象。 这很好用。 但是有一些方法只返回一个int或boolean。 泽西岛无法处理原始类型(据我所知),可能是因为它们没有注释,而泽西没有默认注释。 我通过创建像RestBoolean或RestInteger这样的复杂类型来解决这个问题,它只包含一个int或boolean值并具有相应的注释。 有没有比编写这些容器对象更简单的方法?

捕获所有exception并返回Jersey中的自定义错误

我想在jerseyrest服务中捕捉所有意想不到的例外情况。 因此我写了一个ExceptionMapper: @Provider public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper { private static Logger logger = LogManager.getLogManager().getLogger(ExceptionMapper.class.getName()); @Override public Response toResponse(Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(“Internal error”).type(“text/plain”).build(); } } 映射器捕获所有exception。 所以我写不出来: public MyResult getById(@PathParam(“id”)) { if (checkAnyThing) { return new MyResult(); } else { throw new WebApplicationException(Response.Status.NOT_FOUND); } } 这是由Mapper捕获的。 现在我要写: public Response getById(@PathParam(“id”) { […]

使用jersey-1.7在Google Appengine上进行多部分文件上传

我用Jersey在Google Appengine上写了一个应用程序来处理简单的文件上传。 这在运动衫1.2上运行正常。 在更高版本(当前1.7)中,引入了@FormDataParam来处理多部分/表单输入。 我正在使用jersey-multipart和mimepull依赖。 似乎新的做法是在appengine中创建临时文件,我们都知道这是非法的…… 我是否因为泽西岛现在与AppEngine兼容而错过了什么或做错了什么? @POST @Path(“upload”) @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload(@FormDataParam(“file”) InputStream in) { …. } 使用这些例外调用时,上述操作将失败… /upload java.lang.SecurityException: Unable to create temporary file at java.io.File.checkAndCreate(File.java:1778) at java.io.File.createTempFile(File.java:1870) at java.io.File.createTempFile(File.java:1907) at org.jvnet.mimepull.MemoryData.createNext(MemoryData.java:87) at org.jvnet.mimepull.Chunk.createNext(Chunk.java:59) at org.jvnet.mimepull.DataHead.addBody(DataHead.java:82) at org.jvnet.mimepull.MIMEPart.addBody(MIMEPart.java:192) at org.jvnet.mimepull.MIMEMessage.makeProgress(MIMEMessage.java:235) at org.jvnet.mimepull.MIMEMessage.parseAll(MIMEMessage.java:176) at org.jvnet.mimepull.MIMEMessage.getAttachments(MIMEMessage.java:101) at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:177) at com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80) at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:139) at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:77) at […]

泽西2注射源为multipart formdata

我有一个方法: @POST @Consumes(“multipart/form-data”) @Produces( {“text/xml”}) public Response processForm( @FormDataParam(“myparam”) InputStream is, @FormDataParam(“myparam”) FormDataContentDisposition detail) 这与Jersey 1.x一起运作良好。 我升级到2.0 m11。 现在我收到以下错误: 12/01/2013 11:15:04 AM org.glassfish.jersey.server.ApplicationHandler initialize INFO: Initiating Jersey application, version Jersey: 2.0-m11 2012-12-21 12:34:15… 12/01/2013 11:15:04 AM org.glassfish.jersey.internal.Errors processErrors SEVERE: The following errors and warnings have been detected: WARNING: No injection source found for a parameter […]

泽西岛 – 如何模拟服务

我正在使用“Jersey测试框架”对我的网络服务进行unit testing。 这是我的资源类: import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; // The Java class will be hosted at the URI path “/helloworld” @Path(“/helloworld”) public class class HelloWorldResource { private SomeService service; @GET @Produces(“text/plain”) public String getClichedMessage() { // Return some cliched textual content String responseFromSomeService = service.getSomething(); return responseFromSomeService; } } 如何在unit testing中模拟SomeService?

将BufferedInputStream转换为图像

我无法将我的blob变成缓冲图像,所以我可以使用它。 我从使用inputstream上传的数据库中获取blob(jpg图像)。 在我的数据库中,它存储为BufferedInputStream,我注意到了。 我得到的斑点很好,它是一堆奇怪的符号,并说它是一个jpg所以图像必须很好。 谁能发现我做错了什么? 也许我转换它错了? at image = ImageIO.read(new ByteArrayInputStream(data)); 图像返回null。 @GET @Path(“{id}”) @Produces(“image/*”) public Response post(@PathParam(“id”) String id) throws IOException { Connection con = connection(); Blob blob = getPhoto(con); BufferedImage image = null; byte[] data = null; int blobLength = 0; try { blobLength = (int) blob.length(); data = blob.getBytes(1, blobLength); image = […]