我正在使用Spring Boot创建一个 Restful api服务。一切正常,除了我无法指定要以JSON返回的数据。

我想要的是获取没有字段“content”的输出(在我的情况下,该字段用于存储对象)。

Spring 版本是4.2.5

这就是为什么我定义了3个 View 级别的原因(内部是在数据库中获取/设置对象时要使用的 View ,公共(public)是要输出到客户端的最小数据):

public class EntityVisibility {
    public static class Public { }
    public static class Detailed extends Public { }
    public static class Internal extends Detailed { }
}

我的 Controller :
@RestController
@RequestMapping("/api/photos")
public class PhotoController extends BaseController {

    @JsonView(EntityVisibility.Public.class)
    @RequestMapping(value="/load", method= RequestMethod.GET)
    public Response<Photo> loadFromUrl(@RequestParam("url") String urlAddress) {

        Photo photo = ...; // create photo object

        return new Response<>(photo);
    }
}

在我看来,@JsonView(EntityVisibility.Public.class)现在可以正常工作了。

响应类别(仅相关部分):
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> {

private T result;

    public Response(T response) {
        result = response;
    }


}

我的摄影课(相关部分):
public class Photo extends Entity {

    @JsonIgnore
    protected byte[] content;

    @JsonView(EntityVisibility.Public.class)
    private Date modified;

    @JsonProperty("content")
    @JsonView(EntityVisibility.Internal.class)
    public void setB64content(String b64content) {
        this.content = (b64content == null) ? null : Base64.decode(b64content.getBytes());
    }

    public String getB64content() {

        try {
            return (content.length == 0) ? null : new String(Base64.encode(content), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

}

实体类(相关部分):
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Entity {

}

我得到的是:
{"success":true,"result":{"mime_type":"image/png","content":"SOME_CONTENT_HERE"}}
预期结果:
{"success":true,"result":{"mime_type":"image/png"}}
任何帮助是极大的赞赏!

==编辑==

我创建了一个非常简单的新设置,但对我来说仍然无效:

@RestController中的方法:
@JsonView(EntityVisibility.Public.class)
@RequestMapping(value="/test", method= RequestMethod.GET)
public Response<TestObject> testMethos() {

    TestObject testObject = new TestObject();
    testObject.setTest1("test1");
    testObject.setTest2("test2");
    testObject.setTest3("test3");

    return new Response<>(testObject);
}

测试类:
public class TestObject {

    @JsonView(EntityVisibility.Public.class)
    private String test1;

    @JsonView(EntityVisibility.Detailed.class)
    private String test2;

    @JsonView(EntityVisibility.Internal.class)
    private String test3;

    @JsonIgnore
    private String test4;


    public String getTest1() {
        return test1;
    }

    public void setTest1(String test1) {
        this.test1 = test1;
    }

    public String getTest2() {
        return test2;
    }

    public void setTest2(String test2) {
        this.test2 = test2;
    }

    public String getTest3() {
        return test3;
    }

    public void setTest3(String test3) {
        this.test3 = test3;
    }

    public String getTest4() {
        return test4;
    }

    public void setTest4(String test4) {
        this.test4 = test4;
    }
}

我的pom.xml(完整):
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ch.sumli</groupId>
    <artifactId>SumliCore</artifactId>
    <version>0.1</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>

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

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

        <dependency>
            <groupId>com.couchbase.client</groupId>
            <artifactId>java-client</artifactId>
            <version>2.2.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.2.Final</version>
        </dependency>

        <dependency>
            <groupId>jmimemagic</groupId>
            <artifactId>jmimemagic</artifactId>
            <version>0.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.imgscalr</groupId>
            <artifactId>imgscalr-lib</artifactId>
            <version>4.2</version>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.2</version>
        </dependency>

    </dependencies>

    <properties>
        <start-class>ch.sumli.Application</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <repositories>
        <repository>
            <id>couchbase</id>
            <name>couchbase repo</name>
            <url>http://files.couchbase.com/maven2</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

而且我仍然得到所有东西(@JsonIgnore除外):
{"success":true,"result":{"test1":"test1","test2":"test2","test3":"test3"}}

最佳答案

经过与@kkazakov的评论部分讨论之后,问题似乎出在来自不同Jackson版本的@JsonView中:



旧帖子:

我测试了您的代码,并认为它符合我的预期。至少我看不到内容并获得所需的json。唯一的区别是@JsonView(EntityVisibility.Public.class)中的class Response<T>,但我认为您也有,因为否则您将获得空对象

这是演示项目。您可以在设备上进行测试还是将我指向其他设置?

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    public static class EntityVisibility {
        public static class Public { }
        public static class Detailed extends Public { }
        public static class Internal extends Detailed { }
    }

    @RestController
    public static class MyRestController {

        @JsonView(EntityVisibility.Public.class)
        @RequestMapping(value="/", method= RequestMethod.GET)
        public Response<Photo> loadFromUrl() {

            Photo photo = new Photo(); // create photo object
            photo.setB64content("dfgsdfgsdfg");
            photo.setMime_type("image/png");
            System.out.println(photo);
            return new Response<>(photo);
        }
    }


    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Response<T> {
        @JsonView(EntityVisibility.Public.class)
        private T result;

        @JsonView(EntityVisibility.Public.class)
        private boolean success = true;

        @JsonView(EntityVisibility.Public.class)
        private Object error;

        @JsonView(EntityVisibility.Public.class)
        private String errorCode;

        @JsonView(EntityVisibility.Public.class)
        private String message;

        public T getResult() {
            return result;
        }

        public void setResult(T result) {
            this.result = result;
        }

        public Response(T response) {
            result = response;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public Object getError() {
            return error;
        }

        public void setError(Object error) {
            this.error = error;
        }

        public String getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    public static class Photo extends Entity {

        @JsonIgnore
        protected byte[] content;

        @JsonView(EntityVisibility.Public.class)
        private Date modified;

        @JsonView(EntityVisibility.Public.class)
        private String mime_type;

        @JsonProperty("content")
        @JsonView(EntityVisibility.Internal.class)
        public void setB64content(String b64content) {
            this.content = (b64content == null) ? null : b64content.getBytes();
        }

        public String getB64content() {
            return (content.length == 0) ? null : Base64.encode(content);
        }

        public byte[] getContent() {
            return content;
        }

        public void setContent(byte[] content) {
            this.content = content;
        }

        public Date getModified() {
            return modified;
        }

        public void setModified(Date modified) {
            this.modified = modified;
        }

        public String getMime_type() {
            return mime_type;
        }

        public void setMime_type(String mime_type) {
            this.mime_type = mime_type;
        }

        @Override
        public String toString() {
            return "Photo{" +
                    "content=" + Arrays.toString(content) +
                    ", modified=" + modified +
                    '}';
        }
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Entity {

    }
}

127.0.0.1:8080的结果是:
{
  "result": {
    "mime_type": "image/png"
  },
  "success": true
}

09-26 21:32