小编Who*_*AmI的帖子

JAX-RS jersey ExceptionMappers用户定义的异常

我是新手,试图阅读一些文档,但它不起作用,请耐心等待.

我创建了一个UserNotFoundMapper使用ExceptionMappers这样的:

public class UserNotFoundMapper implements ExceptionMapper<UserNotFoundException> {

@Override
public Response toResponse(UserNotFoundException ex) {
    return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}

}
Run Code Online (Sandbox Code Playgroud)

这在我的服务中:

@GET
@Path("/user")
public Response getUser(@QueryParam("id") String id) throws UserNotFoundException{
    //Some user validation code with DB hit, if not found then
    throw new UserNotFoundException();
}
Run Code Online (Sandbox Code Playgroud)

UserNotFoundException是一个User-Defined例外.

我试过这个:

public class UserNotFoundException extends Exception {
       //SOME block of code 
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用该服务时,UserDefinedExceptionMapper不会被调用.看来我可能会遗漏一些东西UserDefinedException.那么如何定义这个异常呢?

请让我知道如何定义UserNotFoundException.

java rest web-services jax-rs jersey

38
推荐指数
1
解决办法
4万
查看次数

JAX-RS jersey客户端:使用状态代码读取响应

我使用此代码使用泽西客户端调用泽西JAX-RS服务.

public static void main(String[] args) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);

    WebResource service = client.resource(getBaseURI());

    String msg = service.path("rest").path("ExceptionDemo").path("user").queryParam("id", "001").get(String.class);     
    System.out.println(msg);

}

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8045/ExceptionHanlding").build();
}
Run Code Online (Sandbox Code Playgroud)

当响应状态代码为200时,此代码正常工作.但对于200以外的任何其他代码,此代码将引发异常.

如何修改此代码以便根据响应的状态代码执行某些操作?

java rest web-services jax-rs jersey

7
推荐指数
1
解决办法
2万
查看次数

java中序列化的基础知识不明白

我无法理解SerializationJava 1.6 的基础知识.

下面是Dog包含Class的实例变量的CollarClass:

Dog.java

public class Dog implements Serializable {

private Collar collar;

public Collar getCollar() {
    return collar;
}

public void setCollar(Collar collar) {
    this.collar = collar;
}

}
Run Code Online (Sandbox Code Playgroud)

Collar类没有实现Serializable接口,如下所示:

Collar.java

public class Collar {

private int size;

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试序列化时,Dog为什么它不抛出NotSerializableException?根据合同,整个对象图应该实现,Serializable但我的Collar班级不满足.

以下是此演示的主要方法:

public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud)

java serialization servlets web-applications serializable

4
推荐指数
1
解决办法
648
查看次数

在JAX-RS jerey中没有调用异常映射器

我正在尝试配置一个ExceptionMapper,它将捕获我的应用程序中的所有404相关异常.这是我第一次尝试使用这个ExceptionMapper,因此面临很多问题,可能会遗漏一些愚蠢的东西:(

下面是我在ExceptionMapper类中所做的:

public class ClassNotFoundMapper implements ExceptionMapper<NotFoundException> {

@Override
public Response toResponse(NotFoundException ex) {
    return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}
Run Code Online (Sandbox Code Playgroud)

}

web.xml中我添加了这个条目:

<init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            com.service.rest;
            com.service.exception
        </param-value>
    </init-param>
Run Code Online (Sandbox Code Playgroud)

服务中我这样做了:

@GET
@Path("{param}")
public Response getName(@PathParam("param") String msg, @HeaderParam("name") String headerName) {

    //SOME CODE HERE AND THEN CONDITIONALLY RETURN THE EXCEPTION
    return Response.status(Response.Status.NOT_FOUND).entity("NOT FOUND").build(); 
}
Run Code Online (Sandbox Code Playgroud)

不会调用ExceptionMapper中出现的异常.

请让我知道我错过了什么.

java rest web-services jax-rs

3
推荐指数
1
解决办法
3661
查看次数

JAX-RS异常:使用资源的GET进行批注,类不被视为有效的资源方法

我正在使用JAX-RS的平针织实现来进行Web服务.我是这个JAX-RS的新手.

我正在尝试在服务中添加一个接受Employee对象的方法,并根据Employee对象值返回雇员Id(此时有一个DB命中).

遵循Restful原则,我将方法设为@GET并提供了url路径,如下所示:

@Path("/EmployeeDetails")
public class EmployeeService {
@GET
@Path("/emp/{param}")
public Response getEmpDetails(@PathParam("param") Employee empDetails) {

    //Get the employee details, get the db values and return the Employee Id. 

    return Response.status(200).entity("returnEmployeeId").build();

}
}
Run Code Online (Sandbox Code Playgroud)

出于测试目的,我写了这个客户端:

public class ServiceClient {

public static void main(String[] args) {

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());

    Employee emp = new Employee();
    emp.name = "Junk Name";
    emp.age = "20";
    System.out.println(service.path("rest").path("emp/" + emp).accept(MediaType.TEXT_PLAIN).get(String.class));

}

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8045/AppName").build(); …
Run Code Online (Sandbox Code Playgroud)

java rest service web-services jax-rs

2
推荐指数
1
解决办法
2万
查看次数

@RolesAllowed无法用泽西解决

我正在使用JAX-RS使用平针织实现.我正在尝试使用Tomcat 6使用BASIC身份验证来验证我的服务.

这是代码:

@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();
}

}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用注释我的方法时@RolesAllows,我收到编译错误:

@RolesAllows cannot be resolved to a type

请让我知道如何解决这个问题?这需要任何特定的罐子/ API?

编辑:

web.xml中

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            com.security;
            com.exception
        </param-value>
    </init-param>       
    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<security-constraint>      
  <web-resource-collection>
      <web-resource-name>BasicDemo</web-resource-name>          
      <url-pattern>/*</url-pattern>
      <http-method>GET</http-method>
  </web-resource-collection>
  <auth-constraint>
      <role-name>Admin</role-name>
  </auth-constraint>
</security-constraint>
  <login-config>
  <auth-method>BASIC</auth-method>
  <!-- The realm name is typically displayed by the browser in the login …
Run Code Online (Sandbox Code Playgroud)

java rest web-services jax-rs jersey

0
推荐指数
2
解决办法
8065
查看次数