Tag: jax rs

在javax.xml.bind中创建一个通用集合

在我编写的REST服务器中,我有几个集合类来包装要从我的服务返回的单个项目: @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = “person_collection”) public final class PersonCollection { @XmlElement(name = “person”) protected final List collection = new ArrayList(); public List getCollection() { return collection; } } 我想重构这些以使用generics,因此样板代码可以在超类中实现: public abstract class AbstractCollection { protected final List collection = new ArrayList(); public List getCollection() { return collection; } } @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = “person_collection”) public final […]

如何将REST请求转发到另一个资源?

在我目前的架构中,我有一个JAX-RS资源,它位于后面: /categories /categories/{catId} 这是这样实现的: @Path(“/categories”) @Produces(“application/json”) public class CategoryResourcesApi { @GET public Response getCategories() { // … } @GET @Path(“/{catId}”) public Response getCategory(@PathParam(“catId”) String catId) { // … } // … } 和另一个服务: /products /products/{prodId} 并有类似的实现: @Path(“/products”) @Produces(“application/json”) public class ProductResourcesApi { @GET public Response getProducts() { // … } // … } 除了这些直截了当的路径,我还需要服务于这些: /categories/{catId}/products […]

版本化REST API和供应商特定的内容类型

我在这个主题中阅读了很多关于REST API版本的内容: API版本化的最佳实践? 因此,我想使用HTTP-Accept-Header来指示客户端要求的版本。 但是如何在我的应用程序中应用它? 因此,有哪些变化? 编组人员如何知道应该使用哪个版本? 我必须注册我的类型吗? 我所知道的是我必须更改@Produces -Annotation的内容 @GET @Path(“/locations”) @Produces(“application/vnd.mycompany-v1+xml”) Location[] getLocations(); 但还有什么必须改变?

尝试向Jersey注入自定义上下文时缺少字段依赖项

我有一个自定义上下文: public class MyContext { public String doSomething() {…} } 我创建了一个上下文解析器: @Provider public class MyContextResolver implements ContextResolver { public MyContext getContext(Class type) { return new MyContext(); } } 现在在资源中我尝试注入它: @Path(“/”) public class MyResource { @Context MyContext context; } 我收到以下错误: SEVERE: Missing dependency for field: com.something.MyContext com.something.MyResource.context 相同的代码在Apache Wink 1.1.3中运行良好,但在Jersey 1.10中失败。 任何想法将不胜感激。

在TomEE +上使用JAX-RS时,“没有资源方法”

使用库存TomEE +,我无法获得简单的JAX-RS资源。 我不断得到一个错误: Jun 30, 2012 5:09:59 PM org.apache.cxf.jaxrs.utils.ResourceUtils checkMethodDispatcher WARNING: No resource methods have been found for resource class com.tensorwrench.test.BaseResource Jun 30, 2012 5:09:59 PM org.apache.cxf.jaxrs.utils.ResourceUtils checkMethodDispatcher WARNING: No resource methods have been found for resource class com.tensorwrench.test.BaseResource Jun 30, 2012 5:09:59 PM org.apache.cxf.jaxrs.utils.ResourceUtils checkMethodDispatcher WARNING: No resource methods have been found for resource […]

如何使用JAX-RS NewCookie删除服务器上的cookie?

我想删除服务器上的cookie(通过将Expires设置为过去)。 我怎么能用javax.ws.rs.core.NewCookie做到这一点? 我正在尝试这个,但它不起作用: return Response.ok() .entity(“hello world!”) .cookie( new NewCookie( “foo”, “”, “/”, “.example.com”, 1, “no comment”, 0, // maxAge false ) ) .build(); 此代码段生成此HTTP标头: Set-Cookie:foo=;Version=1;Comment=”no comment”;Domain=.example.com;Path=/ 此标头不会从服务器中删除cookie。 什么是可能的解决方法?

为什么使用Jersey在JSON中使用@返回名称

我使用的是JAXB,它是Jersey JAX-RS的一部分。 当我为输出类型请求JSON时,我的所有属性名称都以这样的星号开头, 这个对象; package com.ups.crd.data.objects; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; @XmlType public class ResponseDetails { @XmlAttribute public String ReturnCode = “”; @XmlAttribute public String StatusMessage = “”; @XmlAttribute public String TransactionDate =””; } 成为这个, {“ResponseDetails”:{“@transactionDate”:”07-12-2010″, “@statusMessage”:”Successful”,”@returnCode”:”0″} 那么,为什么名字中有@?

如何使用jax-rs的属性文件?

我刚刚开始使用在Tomcat上运行的jax-rs来设置Web服务。 有没有办法将属性文件与我的java项目(在eclipse中)捆绑在一起,以便我可以在运行时读取它的属性? 如果可能的话,最好放置它的位置(通过url无法看到),Web内容,WEB-INF等? 谢谢。

jax-rs检索表单参数

我正在尝试使用HttpServletRequest检索从已发布表单传递给jax-rs的一些参数。 但是,我的请求对象始终为我的参数返回空值。 我不是以正确的方式去做这件事吗? 我已经发布了下面的代码,以及一个发送的示例请求。 这是我的服务: @Path(“/”) @Stateless public class HomeController { @Context private HttpServletRequest request; @Context private HttpServletResponse response; @EJB private LoginServiceLocal loginService; @POST @Path(“/authenticate”) @Consumes(“application/x-www-form-urlencoded”) public void authenticate() throws Exception { String email = request.getParameter(“email”); String password = request.getParameter(“password”); if (loginService.authenticate(email, password)) { response.sendRedirect(“/app”); } else { request.setAttribute(“authenticationError”, “Invalid email/password.”); } } } 示例请求: […]

基于注释的Spring / JAX-RS集成,没有web.xml

我正在尝试制作一个没有web.xml的Servlet 3.0 REST服务器,即仅依赖于java类和注释。 我不确定如何同时使用Spring和servlet映射注册Resource类。 我目前有: public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[]{RootConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { return new Class[]{RestServletConfiguration.class}; } @Override protected String[] getServletMappings() { return new String[] {“/*”}; } } @Configuration @ComponentScan({“com.my.spring.beans”, “com.my.spring.services”}) public class RootConfiguration { } @Configuration @ComponentScan(“com.my.resource.classes”) public class RestServletConfiguration { } […]