我已经在一年中的大部分时间里使用了泽西,并且偶然发现了一个我无法找到答案的问题:你如何拦截(或挂钩)泽西岛请求生命周期?
理想情况下,我可以在容器接受来自网络的请求和调用处理程序方法的时间之间执行一些自定义过滤/验证/拒绝.如果有一种通过子路径过滤拦截器的简单方法(例如,对于/下的任何内容都有一个拦截器,对于/ user /下的任何东西,等等),可以获得奖励积分.
谢谢!
编辑:为了更清楚一点,这里的一般想法是能够编写一些将为许多API调用运行的代码,而无需从每个处理程序方法显式调用该代码.这将减少额外的代码并消除传递请求上下文的需要.
今天我读到了Restful服务.基本上我所理解的是Restful webservices将在HTTP请求方法上工作,而不是普通的webservice将在SOAP请求上工作.
什么是Restful服务需要,因为普通的servlet也可以在HTTP方法上工作?
我编写了使用REST JAX-RS生成Excel文件的代码,并确认生成的Excel文件位于GlassFish服务器目录中.
但我的目标是当用户点击按钮(生成Excel .xls)时,我想要下载弹出窗口,询问用户是否保存或打开.xls文件,就像任何其他Web服务用于下载任何类型的文件一样.
根据我的搜索,步骤是:
生成Excel .xls(DONE)
将excel写入流
在JAX-RS文件中,将响应头设置为类似的,
String fileName ="Blah_Report.xls"; response.setHeader("Content-Disposition","attachment; filename ="+ fileName);
我的问题是我在JAX-RS文件中做了所有这些,而且我没有可用的HttpServletResponse对象.
根据Add Response Header到JAX-RS Webservice的答案
他说:
您可以通过Web服务中的@Context注释注入对实际HttpServletResponse的引用,并使用addHeader()等来添加标头.
如果没有示例代码,我无法确切地说明这意味着什么.
我从服务器收到以下错误响应.
HTTP状态500 -
类型异常报告
信息
description服务器遇到内部错误(),导致无法完成此请求.
例外
javax.servlet.ServletException:java.lang.UnsupportedOperationException:尝试序列化java.lang.Class:org.hibernate.proxy.HibernateProxy.忘了注册一个类型适配器?
根本原因
java.lang.UnsupportedOperationException:尝试序列化java.lang.Class:org.hibernate.proxy.HibernateProxy.忘了注册一个类型适配器?
从Java调试器:
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer@7632012e
Run Code Online (Sandbox Code Playgroud)
我正在使用Gson将我的Java对象转换为JSON.下面我贴了一些代码.
这是我的资源:
@Stateless
@LocalBean
@Path("/autos")
@Produces(MediaType.APPLICATION_JSON)
public class AutoResource {
@EJB
private CarAssembler warehouse;
@Context
private UriInfo uriInfo;
@GET
public Response allAutos() {
// Building a context, lots of code...
// Creating a Gson instance and configures it...
final Auto auto = warehouse.list(context);
final String autoJson = gson.toJson(auto);
return Response.ok(autoJson).build();
}
}
Run Code Online (Sandbox Code Playgroud)
CarAssembler只是一个调用存储库的服务.我没有在这里粘贴服务代码.
库:
@Override
public Question findById(final int id, final FetchType fetchType) {
final Auto question = …Run Code Online (Sandbox Code Playgroud) 我正在与Grizzly,Jersey和Jackson一起使用REST应用程序,因为Jersey忽略了我的自定义ObjectMapper.
POM依赖:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
结果版本是:Grizzly 2.3.3,Jackson 2.1.4和Jersey 2.2.
主类(我希望明确注册Jersey组件):
public class Main {
public static void main(String[] args) {
try {
ResourceConfig rc = new ResourceConfig();
rc.register(ExampleResource.class);
rc.register(ObjectMapperResolver.class);
HttpHandler handler = ContainerFactory.createContainer(
GrizzlyHttpContainer.class, rc);
URI uri = new URI("http://0.0.0.0:8080/");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri);
ServerConfiguration config = server.getServerConfiguration();
config.addHttpHandler(handler, "/");
server.start();
System.in.read();
} catch (ProcessingException | URISyntaxException | IOException e) {
throw new Error("Unable to create HTTP server.", e);
}
} …Run Code Online (Sandbox Code Playgroud) 我正在考虑使用websockets和http构建REST API,我使用websockets告诉客户端新数据可用或直接向客户端提供新数据.
以下是它如何工作的一些不同的想法:
ws = websocket
想法A:
GET /usersPOST /usersGET /users想法B:
GET /users/usersPOST /users想法C:
GET /users/usersPOST /users它获得id 4GET /users/4我死了:
GET /users/users.POST /users/usersGET /users?lastcall='time of step one'哪种选择最好,有哪些优缺点?
这是另一个更好的'Idea E'吗?
我们甚至需要使用REST还是需要所有数据?
编辑
为了解决数据不同步的问题,我们可以提供标题
"If-Unmodified-Since"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since
或"E-Tag"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
或两者都有PUT请求.
我正在尝试通过REST Web服务传递对象.以下是我的课程使用一些示例代码解释了我需要的功能.
Rest Web Service Class方法
@POST
@Path("/find")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_JSON})
public Response getDepartments(){
Response response = new Response();
try {
response.setCode(MessageCode.SUCCESS);
response.setMessage("Department Names");
Department dept = new Department("12", "Financial");
response.setPayload(dept);
} catch (Exception e) {
response.setCode(MessageCode.ERROR);
response.setMessage(e.getMessage());
e.printStackTrace();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
响应类
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Response implements Serializable{
private static final long serialVersionUID = 1L;
public enum MessageCode {
SUCCESS, ERROR, UNKNOWN
}
private MessageCode code;
private String message;
private Object payload;
public MessageCode getCode() …Run Code Online (Sandbox Code Playgroud) 我首先在stackoverflow中进行了搜索,但我无法找到与我的问题相关的任何答案.我能找到的只是与REST uri设计相关的问题.
我在后端的问题.假设我们有两个不同版本的REST uri
http://api.abc.com/rest/v1/products
http://api.abc.com/rest/v2/products
在后端方面(服务器端代码)遵循基于版本的这两组API的正确路由,可管理性和现有类的重用的最佳方法是什么?
我已经考虑过使用不同的@Path注释定义资源类的方法,例如,在该包的ProductsResource类中单独包含v1和v2的包,定义
package com.abc.api.rest.v1.products;
@Path("/rest/v1/products")
public class ProductsResource {...}
package com.abc.api.rest.v2.products;
@Path("/rest/v2/products")
public class ProductsResource {...}
Run Code Online (Sandbox Code Playgroud)
然后有基于版本的实现逻辑.这种方法的问题是当我们只从api集合中改变一个特定的资源api时,我们也必须将其他类复制到v2包中.我们能避免吗?
怎么写自定义注释说@Version并拥有它支持的版本的值?现在无论是v1还是v2,两个请求都将转到相同的资源类.
比如说
package com.abc.api.rest.products;
@Path("/rest/{version: [0-9]+}/products")
@Version(1,2)
public class ProductsResource {...}
Run Code Online (Sandbox Code Playgroud)
更新:
Jarrod有一个API版本控制建议来处理标题中的版本.这也是一种方法,但我期待在遵循基于URI的版本控制时使用最佳实践
我有一个使用Jersey构建的REST服务.
我希望能够根据发送到服务器的MIME设置我的自定义异常编写器的MIME.application/json收到json application/xml时,以及收到xml 时返回.
现在我硬编码application/json,但这使得XML客户端陷入了困境.
public class MyCustomException extends WebApplicationException {
public MyCustomException(Status status, String message, String reason, int errorCode) {
super(Response.status(status).
entity(new ErrorResponseConverter(message, reason, errorCode)).
type("application/json").build());
}
}
Run Code Online (Sandbox Code Playgroud)
我可以利用什么上下文来获取当前请求Content-Type?
谢谢!
对于任何对完整解决方案感兴趣的人:
public class MyCustomException extends RuntimeException {
private String reason;
private Status status;
private int errorCode;
public MyCustomException(String message, String reason, Status status, int errorCode) {
super(message);
this.reason = reason;
this.status = status;
this.errorCode = errorCode;
}
//Getters and setters
} …Run Code Online (Sandbox Code Playgroud) 我的服务定义如下.
public String getData(@QueryParam("date") Date date)
我正试图java.util.Date从我的客户端(它是jaxrs:CXF的客户端,而不是通用HTTP客户端或浏览器)传递给它.
我的服务收到Thu Mar 01 22:33:10 IST 2012HTTP URL中的日期.由于CXF无法Date使用此String 创建对象,因此我的客户端收到404错误.我尝试ParameterHandler在服务端使用a ,但我仍然无法成功解析它,因为我不期望任何特定格式的日期.
根据这篇文章,传递一个Date应该是开箱即用,但我似乎无法让基本案例工作.我是否需要做任何事情才能成功地将Date对象从我的客户端传递给服务?感谢任何帮助.
谢谢