小编Dom*_*nic的帖子

捕获所有异常并返回Jersey中的自定义错误

我想在球衣休息服务中抓住所有意想不到的例外情况.因此我写了一个ExceptionMapper:

@Provider
public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<Exception> {
    private static Logger logger = LogManager.getLogManager().getLogger(ExceptionMapper.class.getName());

    @Override
    public Response toResponse(Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);

        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal error").type("text/plain").build();
    }
}
Run Code Online (Sandbox Code Playgroud)

映射器捕获所有异常.所以我写不出来:

public MyResult getById(@PathParam("id")) {
    if (checkAnyThing) {
        return new MyResult();
    }
    else {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是由Mapper捕获的.现在我要写:

public Response getById(@PathParam("id") {
    if (checkAnyThing) { {
        return Response.ok().entity(new MyResult()).build();
    }
    else {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是捕获所有意外异常并正确返回错误(错误代码)的正确方法吗?或者还有其他(更正确的)方式吗?

java jax-rs jersey jersey-2.0

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

JerseyTest和JUnit抛出NullPointerException

我在球衣测试框架上遇到了一些问题.如果我使用@Before和@After注释,则target方法抛出NullPointerException.我认为JerseyTest适用于JUnit?我的问题在哪里?

  • 泽西岛:2.12
  • JUnit:4.11

代码失败:

public class MyResourceTest extends JerseyTest {
    @Before
    public void setUp() { }

    @After
    public void tearDown() { }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

位于org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566)的java.lang.NullPointerException,位于foo.bar.MyResourceTest的org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580). SHOULD_RETURN_BAD_REQUEST(MyResourceTest.java:43)

有效的代码:

public class MyResourceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

JerseyWebTarget { http://localhost:9998/myPath }
Run Code Online (Sandbox Code Playgroud)

java junit overriding junit4 jersey-2.0

12
推荐指数
2
解决办法
3427
查看次数

使用Java将xml转换为json

有没有办法将xml文件转换为json?XML可以是任何结构,因此没有用于实例化的POJO类.我需要将xml转换为json或转换为没有根节点的Map.

例如:

<import name="person">
    <item>
        <firstName>Emil</firstName>
        <lastName>Example</lastName>
        <addresses>
            <address>
                <street>Example Blvd.</street>
            </address>
            <address>
                <street>Example Ave.</street>
            </address>
        </addresses>
    </item>
</import>
Run Code Online (Sandbox Code Playgroud)

预期的JSON

{
    "firstName": "Emil",
    "lastName": "Example",
    "addresses": [
        { "street" : "Example Blvd." },
        { "street" : "Example Ave." }
    ]
}
Run Code Online (Sandbox Code Playgroud)

java xml json

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

Google Play 商店的搜索 API

Google Play 商店有搜索 API 吗?我正在寻找类似 Apple Search API 的东西。

api android google-play

5
推荐指数
1
解决办法
7429
查看次数

标签 统计

java ×3

jersey-2.0 ×2

android ×1

api ×1

google-play ×1

jax-rs ×1

jersey ×1

json ×1

junit ×1

junit4 ×1

overriding ×1

xml ×1