springboot+jersey

  依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

<!-- 使用JAXB 在Jersey中将对象转换为XML -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
</dependency>

  jersey 配置类

/**
 * 配置 jersey rest api 资源路径, Spring Boot建议在使用
 * ResourceConfig添加资源类的时候,不要使用packages方法去自动扫描,建议还是使用register添加。
 * 
 * 注解ApplicationPath,默认为/*
 * 
 */
@Component
@ApplicationPath("/jersey")
public class JerseyConfig extends ResourceConfig  {
    
    public JerseyConfig() {
        //packages("com.oy"); // 扫描 com.oy 包,使其识别 JAX-RS 注解
        register(UserController.class);
    }
    
}

  模型类

springboot+jersey
package com.oy.model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "user") // 将该类转化成XML时,说明这个是XML的根节点
public class User {
    private String name;
    private String email;
    
    public User() {}
    
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    @XmlAttribute // 属性
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement // 节点
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
View Code

  资源类

@Component
@Path("/users")
public class UserController {

    /**
     * http://localhost:8080/jersey/users/json
     */
    @GET
    @Path("/json")
    @Produces(MediaType.APPLICATION_JSON)
    public User getJson() {
        List<User> users = new ArrayList<>();
        User user = new User("xxx", "xxx@163.com");
        users.add(user);
        return users.get(0);
    }

    /**
     * 使用 JAXB 在 Jersey 中将对象转换为 XML
     */
    @GET
    @Path("/xml")
    @Produces(MediaType.APPLICATION_XML)
    public User getXml() {
        List<User> users = new ArrayList<>();
        User user = new User("xxx", "xxx@163.com");
        users.add(user);
        return users.get(0);
    }

    // 返回多节点xml
    @GET
    @Path("/getUsers")
    @Produces(MediaType.APPLICATION_XML)
    public List<User> getAllUser() {
        List<User> users = new ArrayList<User>();
        users.add(new User("001", "HuiJia"));
        users.add(new User("002", "Andy"));
        users.add(new User("003", "BoWen"));
        return users;
    }

    // 返回单节点xml
    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("/getUser")
    public User getUser() {
        User user = new User("004", "Lucy");
        return user;
    }

}

 

问题:jersey:MessageBodyWriter not found for media type=application/xml

<!-- 使用JAXB 在Jersey中将对象转换为XML -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
</dependency>

 

上一篇:hadoop入门(10):hdfs的java编程-文件的上传与下载


下一篇:相关性搜索简介——常规技术与应用