Tag: jersey

如果接收到无效的JSon,则不会调用ExceptionMapper

我使用Jersey 2.5.1和Jackson 2.2。 用于构建JSON Rest Web服务。 我有点了,并且运行了2个ExceptionMappers但是对于“not-json”请求,应用程序没有抛出任何exception! ExceptionMapper如果抛出NullPointerException,将调用它 如果JSon Mapping出现问题,将调用ExceptionMapper 我的问题:1。请求正文:{}工作2.请求正文:{}与应用程序端NullPointer调用第一个exception映射器3.请求正文:“jibberish”不调用任何东西(没有被任何ExceptionMapper捕获)导致没有exception被抛出。 不幸的是,响应主体是这样的:无法识别的字段“xxx”(类com.sample.MyDto),未标记为可忽略(9个已知属性……)但我想自定义错误消息,因为我总是返回一个JSon对象。

由于资源方法内部的会话,泽西rest测试失败

我有泽西restapi,但当我尝试测试它失败,因为我在那里得到会话数据,所以问题是,我怎么能模拟或忽略这个会话变量,泽西岛无法检测到? 这是我的测试请求: User response = target(“/am/users/” + userId).request().get(new GenericType() { }); 这是我的资源: @GET @Path(“{userId}”) @Produces(MediaType.APPLICATION_JSON + “;charset=utf-8”) public User getUser(@PathParam(“userId”) String userId, @Context HttpServletRequest request) { User supportUser = (User)request.getSession().getAttribute(“USER”); // Here is where it fails. User user = userDao.getUser(userId, supportUser); return user; }

jersey客户端可以使用JSON将JAXB对象发布到服务器吗?

我找到了很多关于如何设置jersey服务器以便它可以生成和使用JAXB绑定对象的示例,但是我很难找到如何让客户端发布相同的JAXB绑定对象的示例。 此示例显示如何使用XML执行此操作 。 我正在寻找一个展示如何使用JSON的方法。 我甚至不确定这是否可行。 post方法的javadoc是不明确的。 我的post看起来像这样: Client client = Client.create(); WebResource resource = client.resource(uri); ClientResponse response = resource.type(MediaType.APPLICATION_JSON) .post(ClientResponse.class, instanceWithXmlRootElementAnnotation); 当我尝试这个时,我的服务器获取请求,但@FormParam的字段总是作为空发送。 这是我的服务器端方法的签名: @POST @Path(“apath”) @Consumes(MediaType.APPLICATION_JSON) public String postAPath(@FormParam(“InstanceWithXmlRootElementAnnotation”) InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) { //instanceWithXmlRootElementAnnotation is always null 我想知道的是,我是否应该使用instanceWithXmlRootElementAnnotation 。 如果这是传统的Web服务,我将使用JAXB 生成一个对象供客户端使用并通过生成的类发送。 但是从我从我链接的示例中收集的内容来看,这个人正在发送源代码,而不是生成的类。

org.postgresql.util.PSQLException:错误:语法错误在或附近“(”

我曾经发过一个Web应用程序,可以通过maven eclipse在Heroku上部署。 Group Id:org.glassfish.jersey.archetypes 工件ID:jersey-heroku-webapp 版本:2.17 我测试了localhost和POSTMAN上的应用POSTMAN ,它运行正常。 我将它推送到heroku以在servlet容器上测试它,但我得到520 OK 520它只是我在SQLEXCEPTION中返回的数字。 在Heroku日志中我发现了这个错误: 2015-05-13T13:10:37.364388+00:00 app[web.1]: at java.lang.Thread.run(Thread.j ava:745) 2015-05-13T13:10:37.389547+00:00 app[web.1]: org.postgresql.util.PSQLException: ERROR: syntax error at or near “(” 2015-05-13T13:10:37.389560+00:00 app[web.1]: Position: 45 2015-05-13T13:10:37.389740+00:00 app[web.1]: at org.postgresql.core.v3.QueryE xecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) 数据库类: public class Database { public Database() { } public void drivercConnection() { try { Class.forName(“com.mysql.jdbc.Driver”); System.out.println(“jar works 🙂 […]

@RolesAllowed无法用泽西解决

我正在使用JAX-RS使用平针织实现。 我正在尝试使用Tomcat 6使用BASIC身份validation来validation我的服务。 这是代码: @Path(“/authenticate”) @RolesAllowed({“Admin”,”Guest”}) public class BasicAuthenticationSecurity { @GET @Path(“/wbiPing”) @Produces(MediaType.TEXT_PLAIN) @RolesAllowed(“Admin”) public Response wbiPing(){ System.out.println(“Pinged!!!”); return Response.ok(“Pinged!!!”).build(); } } 当我尝试使用@RolesAllows注释我的方法时,我收到编译错误: @RolesAllows cannot be resolved to a type 请让我知道如何解决这个问题? 这需要任何特定的jar子/ API? 编辑: web.xml中 jersey-serlvet com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages com.security; com.exception com.sun.jersey.spi.container.ResourceFilters com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory 1 jersey-serlvet /* BasicDemo /* GET Admin BASIC Login 请让我知道这个问题。

Google App Engine Jersey错误格式json

我正在使用带有Jersey和JAX-RS的Google App Engine JAVA开发REST API。 我希望能够以JSON格式向用户发送自定义错误,因为我正在使用javax.ws.rs.ext.ExceptionMapper 当我在本地计算机上使用Jetty运行应用程序时,一切正常,但是当我部署到Google时,我得到了默认的HTML 404页面 这是资源代码: @GET @Produces(MediaType.APPLICATION_JSON) @Path(“{id}”) public Customer getCustomer(@PathParam(“id”) String id) { Customer customer = getCustomer(id); if(customer == null) throw new NotFoundException(“Customer not found”); return customer; } exception映射器: @Provider public class NotFoundExceptionMapper implements ExceptionMapper { @Override public Response toResponse(NotFoundException e) { ErrorMessage errorMessage = new ErrorMessage(); errorMessage.setErrrorMessage(e.getMessage()); errorMessage.setResponseCode(404); return […]

在头文件中发送params并在服务器端获取它们 – java

我正在使用泽西岛,我希望从请求标题中获取参数; 这是我的java服务器端代码: @GET @Path(“/usersForMobile”) @Produces({ MediaType.APPLICATION_JSON + “;charset=utf-8” }) public Response getUsersForMobile(@Context UriInfo info) { String rashutId ; String userName ; String password; List inspectorList= new ArrayList(); try { rashutId = info.getQueryParameters().getFirst(“rashutId”); userName = info.getQueryParameters().getFirst(“userName”); password = info.getQueryParameters().getFirst(“password”); inspectorList = LoginService.getInspectorsList(userName,password,rashutId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(!inspectorList.isEmpty()) return […]

泽西rest服务显示exceptionjavax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException

我正在研究jersey服务,我在这里提到它在我返回一个java对象时工作正常。 后来我试图让java对象通用它给出exceptionjavax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException @XmlRootElement public class AppObject implements Serializable { private List list; private String license; public AppObject() { list = new ArrayList(); } public AppObject(List list) { this.list = list; } @XmlAnyElement(lax = true) public List getList() { return list; } public void setList(List list) { this.list = list; } public String getLicense() { return […]

Jersey REST:如何编写泽西方法,包括validationHTTP授权

我正在编写一个Restful webservice方法,它首先需要授权…例如findItems方法……在Http Authorization中需要用户名和密码示例代码: @GET @Produce(MediaType.APPLICATION_JSON) public String findItems(){ … } 如何在方法执行前validationhttp授权…

如何处理JAX-RS中的意外exception

我的技术堆栈是Tomcat(servlet 3.0),Jersey用于JAX-RS,Spring和Hibernate。 当Hibernate中的某些数据转换错误出现意外问题时,我不希望客户端看到我的堆栈跟踪是否存在Tomcat尝试默认打印的exception。 但是,我想记录这些exception,以便找出错误并解决问题。 我的第一次尝试是使用来自JAX-RS的ExceptionMapper,并天真地认为可以解决我的问题。 但后来我发现泽西岛抛出了自己的例外,例如未映射的url是com.sun.jersey.api.NotFoundExceptions。 这会导致记录404exception,这是我不想要的。 更糟糕的是,客户端不再获得404而是获得状态代码500。 我可以为jersey抛出的exception创建exception映射器,但我可能会错过一些东西。 这样的事情有最好的做法吗?