Tag: jax rs

@Context对象来自哪里

我一直在寻找,但似乎无法找到明确的答案…… 服务器(我的问题的玻璃鱼)注入用@Context注释的实际objets的机制是什么? 更具体地说,如果我想编写一个类似于以下内容的类: @Path(“/”) public class MyResource { @GET public String doSomething(@Context MyObject obj) { // … } } 那我该怎么办? MyObject在哪里实现,谁在做,以及如何实现? 编辑:我见过以下内容: 在JAX-RS中使用@ Context,@ Provider和ContextResolver http://jersey.576304.n2.nabble.com/ContextResolver-confusion-td5654154.html 但是,这与我所看到的不一致,例如在org.neo4j.server.rest.web.RestfulGraphDatabase的构造函数中,它具有以下签名: public RestfulGraphDatabase( @Context UriInfo uriInfo, @Context Database database, @Context InputFormat input, @Context OutputFormat output, @Context LeaseManager leaseManager )

使用JAX-RSinheritance

我正在使用JAX-RS作为我的Web服务。 我有共同的function,并希望使用inheritance。 我提供简单的CRUD操作。 我已经定义了这样的接口: public interface ICRUD { @POST @Consumes(“application/json”) @Produces(“application/json”) @Path(“create”) public String createREST(String transferObject); @GET @Consumes(“application/json”) @Produces(“application/json”) @Path(“retrieve/{id}”) public String retrieveREST(@PathParam(“id”) String id); @POST @Consumes(“application/json”) @Produces(“application/json”) @Path(“update”) public void updateREST(@Suspended final AsyncResponse asyncResponse, final String transferObject) ; @DELETE @Consumes(“application/json”) @Produces(“application/json”) @Path(“delete/{id}”) public String deleteREST(@PathParam(“id”) String id); } 我有一个实现这个接口的抽象类: public abstract class BaseREST implements […]

在Swagger-UI中对API方法进行排序

我找不到任何工作示例,如何实现以下内容:我希望我的Swagger-UI中的API方法按方法(GET-POST-PUT-DELETE)或/按字母顺序排序。 到目前为止,所有方法都以随机顺序显示,甚至不按给定源代码的顺序显示。 我使用Jax-RS + Jersey 1。 使用@ApiOperation的position属性进行排序对我来说不是一个选项,因为有太多方法且API仍在扩展,所以如果有新的方法我需要更新所有方法。 任何提示?

在Content-Type中指定charset时,Jersey和@FormParam无法正常工作

看起来Jersey 2.0(使用servlet 3.1)在Content-Type标头中指定charset属性时无法解码参数。 例如,考虑以下端点: @POST @Path(“/hello”) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Response hello(@FormParam(“name”) String name) { System.out.println(name); return ok(); } 此curl请求有效: curl -X POST -H “content-type: application/x-www-form-urlencoded” -d “name=tom” http://localhost:8080/sampleapp/hello 相反,以下请求不起作用, name参数为null : curl -X POST -H “content-type: application/x-www-form-urlencoded; charset=UTF-8” -d “name=tom” http://localhost:8080/sampleapp/hello 我认为内容类型中的charset=UTF-8添加会破坏我的代码。 编辑: 我打开了官方机票以防这是一个错误: https : //java.net/jira/browse/JERSEY-1978

带嵌入式服务器的JAX-RS

澄清:这个问题是关于GZIPping基于JAX-WS的REST服务,但我决定改变主题以便更容易找到 我正在通过JAX-WS Provider 实现REST服务,并使用标准Endpoint发布它(原因是我想避免使用servlet容器或应用程序服务器)。 有没有办法使服务器gzip响应内容,如果Accept-Encoding: gzip存在? 如何 由nicore提供的nicore实际上是nicore ,它允许您在没有servlet容器的情况下在嵌入式轻量级服务器之上制作JAX-RS样式的服务器,但是有一些时刻需要考虑。 如果您更喜欢自己管理类(并在启动期间节省时间),您可以使用以下内容: 例 JAX-RS你好世界级: @Path(“/helloworld”) public class RestServer { @GET @Produces(“text/html”) public String getMessage(){ System.out.println(“sayHello()”); return “Hello, world!”; } } 主要方法: 对于简单服务器: public static void main(String[] args) throws Exception{ DefaultResourceConfig resourceConfig = new DefaultResourceConfig(RestServer.class); // The following line is to enable GZIP when client accepts it resourceConfig.getContainerResponseFilters().add(new […]

使用JAX-RS将JSON查询参数转换为对象

我有一个JAX-RS资源,它将其参数作为JSON字符串,如下所示: http://some.test/aresource?query={“paramA”:”value1″, “paramB”:”value2″} 在这里使用JSON的原因是查询对象在实际用例中可能非常复杂。 我想将JSON字符串转换为Java对象,在示例中为dto: @GET @Produces(“text/plain”) public String getIt(@QueryParam(“query”) DataTransferObject dto ) { … } JAX-RS是否支持将JSON作为查询参数传递给Java对象的转换?

在找不到REST资源时返回404是否正确?

假设我有一个简单的Jersey REST资源,如下所示: @Path(“/foos”) public class MyRestlet extends BaseRestlet { @GET @Path(“/{fooId}”) @Produces(MediaType.APPLICATION_XML) public Response getFoo(@PathParam(“fooId”) final String fooId) throws IOException, ParseException { final Foo foo = fooService.getFoo(fooId); if (foo != null) { return Response.status(Response.Status.OK).entity(foo).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } } 基于上面的代码,返回NOT_FOUND状态( 404 )是否正确,或者我应该返回204 ,还是其他一些更合适的代码? 提前谢谢了!

RESTful webservice:如何在java中设置头文件以接受Access-Control-Allow-Origin允许的XMLHttpRequest

我有一个RESTful webservice,它将返回字符串,它是用Java(JAX-WS)编写的。 我的问题是当我使用以下URL向该webservice发送请求时: http://localhost:8080/project/webservices/getlist/getListCustomers 在控制台中,它给出了以下错误消息: XMLHttpRequest无法加载url Access-Control-Allow-Origin不允许使用origin localhost 我该如何处理这个问题? Java代码: @GET @Path(“/getsample”) public Response getMsg() { String output = “Jersey say : ” ; return Response.status(200).entity(output).build(); }

尝试将文件上传到JAX-RS(泽西岛)服务器

我正在尝试使用带有Jersey的multipart / form-data客户端上传文件和其他表单数据。 我也使用Jersey上传到REST Web服务。 这是服务器代码: @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public String create(@FormDataParam(“file”) InputStream file, @FormDataParam(“file”) FormDataContentDisposition fileInfo, @FormDataParam(“name”) String name, @FormDataParam(“description”) String description) { Ingredient ingredient = new Ingredient(); ingredient.setName(name); ingredient.setDescription(description); ingredient.setImageName(fileInfo.getFileName()); ingredient.setImagePath(context.getRealPath(“/resources/uploads/”)); // TODO save the file. try { JSONObject json = new JSONObject(); try { ingredientService.create(ingredient); } catch (final InvalidParameterException ex) { logger.log(Level.INFO, […]

RESTEasy客户端框架身份validation凭据

RESTEasy(一个JAX-RS实现)有一个很好的客户端框架,例如: ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri); 如何为此客户端提供HTTP身份validation凭据?