返回json响应但正确返回xml响应时,找不到返回404的球衣 [英] jersey returning 404 not found when returns json response but returns xml response correctly

查看:59
本文介绍了返回json响应但正确返回xml响应时,找不到返回404的球衣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

返回xml响应时运行代码,但取消注释 jersey-media-moxy 并返回JSON响应后,邮递员给我404找不到原因?

Code is run when return xml response but after uncomment jersey-media-moxy and return JSON response the postman give me 404 not found why?

我的邮件资源类

@Path("messages")
public class MessageResource {

    MessageService service = new MessageService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<MessageModel> getMessage() {
        return service.allMessages();
    }

    @GET
    @Path("{messageId}")@Produces(MediaType.APPLICATION_JSON)
    public MessageModel getMessage(@PathParam("messageId") int id) {
        return service.getMessage(id);
    }
}

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.wiza.ws</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>

<build>
    <finalName>messenger</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency> 

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

</dependencies>
<properties>
    <jersey.version>2.16</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

推荐答案

最可能的原因是JSON序列化发生错误,而MOXy引发了错误.错误是什么,谁知道.您尚未提供足够的信息来确定这一点.我建议注册一个通用的 ExceptionMapper .

The most likely reason is that there is an error happening with the JSON serialization and MOXy is throwing an error. What the error is, who knows. You haven't provided enough information to determine that. What I would suggest is registering a generic ExceptionMapper.

@Provider
public class DebugMapper implements ExeptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable t) {
        t.printStackTrace();
        return Response.serverError()
            .entity(t.getMessage())
            .build();
    }
}

在您的应用程序中注册该错误之后,如果出现 错误,而另一个异常映射器尚未处理该错误,则应该看到堆栈跟踪.

Once you register this with your application, if there is an error not already handled by another exception mapper, you should see the stack trace.

就404而言,这很可能是因为您没有设置错误页面.因此,当servlet容器查找错误页面时,您会在错误页面上看到404,而不是Jersey资源方法上.

As far as the 404, this is most likely because you don't have an error page set up. So when the servlet container is looking for the error page, you get a 404 on the error page, not on the Jersey resource method.

但是,实际上,您不希望出现错误页面.您真的想要Http状态.转到错误页面的最可能原因是因为您需要设置属性

But really, you don't want the error page. You really want the Http status. The most likely reason you are getting forwarded to an error page is because you need to set the property

阅读链接,它将向您说明所有内容.您需要在web.xml或 ResourceConfig 中将此属性配置为 true .如果您使用的是web.xml,则只需将其设置为init-param.常量的字符串值为

Read the link, it will explain to you what it's all about. You need to configure this property to true either in your web.xml or in your ResourceConfig. If you're using a web.xml, then just set it as an init-param. The string value of the constant is

jersey.config.server.response.setStatusOverSendError

如果您使用的是 ResourceConfig ,只需在ResourceConfig中调用 property(key,value)即可进行设置.

If you are using a ResourceConfig, the just set it by calling property(key, value) in the ResourceConfig.

这篇关于返回json响应但正确返回xml响应时,找不到返回404的球衣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆