7

I know this may sound like a duplicate of this or some others, but bear with me.

I have a very basic JAX-RS resource, have added all the required annotations that I saw in this tutorial I followed here.
But I keep getting HTTP Status 500 and the following log output in Eclipse's console.

Mar 18, 2021 1:35:23 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/xml, type=class com.varun.demorest.model.User, genericType=class com.varun.demorest.model.User.

Using Maven, but even after adding most suggestions I am finding on similar questions, I see that it was all mostly already included under

<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>

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>com.varun</groupId>
    <artifactId>demorest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demorest</name>

    <build>
        <finalName>demorest</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.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
                
        <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>3.0.0</version>
        </dependency>
                
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>   
    </dependencies>
    <properties>
        <jersey.version>3.0.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

My Model class:
User.java:

package com.varun.demorest.model;

import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {
    private String name;
    private String phone;
    
    public User() {
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }
    public String getPhone() {
        return phone;
    }
}

CustomResource.java:

package com.varun.demorest;

import com.varun.demorest.model.User;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("getUser")
public class CustomResource {
    
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public User getUser() {
        
        System.out.println("getUser Called!");
        User user = new User();
        user.setName("Varun");
        user.setPhone("xxxxxxxxxx");
        System.out.println(user);
        return user;
    }
}

I am unexperienced in JAX-RS, so any help is much appreciated.
Using Java 11 and Tomcat 10.

Varun Gawande
  • 870
  • 9
  • 21
  • Can you try to use `javax.xml.bind.annotation.XmlRootElement` and see what happens. – Paul Samsotha Mar 20 '21 at 05:45
  • @PaulSamsotha That's actually what I had tried first. But ```javax.xml.bind.annotation.XmlRootElement``` has been depreacated and discarded for a good while. The binds were moved to ```jakarta.xml.bind.annotation.XmlRootElement``` – Varun Gawande Mar 20 '21 at 16:47
  • Everything looks good. Maybe try to change the maven-compiler to use Java 11. You say you're using Java 11, but the Maven configuration shows you're using Java 7. – Paul Samsotha Mar 20 '21 at 16:53
  • @PaulSamsotha I changed the `````` and `````` values inside the build configuration in the pom.xml from 1.7 to 11. Unfortunately, no change. – Varun Gawande Mar 20 '21 at 17:57
  • [Try this](https://stackoverflow.com/a/51586202/2587435) – Paul Samsotha Mar 20 '21 at 18:58
  • 1
    You can try to add Jersey-media-jaxb but I’m pretty sure it’s already pulled in as a transitive dependency. – Paul Samsotha Mar 21 '21 at 14:48

7 Answers7

6

as say @Paul Samsotha, it solve this problem for me, I append in pom.xml this:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.2</version>
</dependency>

and it fix problem.

Lubagov
  • 91
  • 1
  • 5
  • I'm still on version 2 (`2.39` right now) of Jersey, but also here XML stopped working at some point. Adding `jersey-media-jaxb` as dependency solved the problem. I checked before, and it was not pulled in as transitive dependency. – Sky Feb 27 '23 at 07:37
3

I was following the same tutorial and having the same issue, I fixed it by adding the following dependencies:

`       <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>3.0.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
            <version>3.0.2</version>
        </dependency>
` 
Sasori123
  • 41
  • 3
2

was running a similar code in Eclipse 2022-03, ran into same error fixed it with these three dependencies

<dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>3.0.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-jaxb</artifactId>
        <version>3.0.2</version>
    </dependency>

all before I was able to return Xml data, I wonder why these works

littleworm
  • 21
  • 3
  • This is a duplicate of [this other answer](https://stackoverflow.com/a/70374216/771431) – xonya May 21 '22 at 22:09
2

I am on JDK 17 and Tomcat 10, faced the same issue. Adding the following dependencies worked for me to enable JAXB without any error. (If you don't have any of these - some or the other error would come)

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.0</version>
</dependency>
1

I have used the jaxb-runtime and jersey-media-jaxb jars which fixed the error.

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>4.0.0-M4</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.4</version>
</dependency>
Tharun
  • 11
  • 2
1

added @jakarta.xml.bind.annotation.XmlRootElement in pojo class. and in pom.xml added below dependencies

 <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>4.0.0-M4</version>
</dependency>

this resolved issue.

Lalitha
  • 11
  • 2
0

Instead of using @XmlRootElement, use @jakarta.xml.bind.annotation.XmlRootElement This is such a silly thing that solved my problem. This is so frustrating I spent hours solving this and this freaking small thing solved it. Please confirm if my answer helped you. Thanks.

vaibhavn1
  • 9
  • 4