Moataz.Osman
Moataz.Osman

Reputation: 43

jersey returning 404 not found when returns json response but returns xml response correctly

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?

my message resource class

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

my 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>

Upvotes: 0

Views: 492

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

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.

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.

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

ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR

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

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

Upvotes: 1

Related Questions