无法将JSON映射到POJO

时间:2017-11-17 02:33:21

标签: java json spring rest api

  • 我有以下控制器
  • 我正在将N2NPath作为@RequestBody对象传递(因为如果我将它用作@RequestBody,则spring不会映射接口IPath)
  • 所以我正在使用实现IPath的N2NPath
  • 但我仍然收到Bad request as response

注意:N2NPath和IPath来自第三方库

Response: <!DOCTYPE html><html><head><title>Error report</title></head><body><h1>HTTP Status 400 - </h1></body></html>

Spring Rest Controller:

        @ApiOperation(value = "get the connections between focus organization and target organization")
        @RequestMapping(value = "/orgorg-connections/{focusOrgId}/{targetOrgId}", method = RequestMethod.POST)
        public @ResponseBody Set<ConnectionTo> getorgToOrgConnections(@RequestBody N2NPath IPath,@PathVariable Long focusOrgId,
                @PathVariable Long targetOrgId) {
            LOGGER.info("In org to org ");
            if (!Util.isNull(focusOrgId) && !Util.isNull(targetOrgId)) {
                try {
                    return orgService.getOrgToOrgConnections(focusOrgId, targetOrgId, IPath);
                } catch (Exception e) {
                    LOGGER.error("error occured while getting orgtoorg connections {}" + e);
                    LOGGER.info("For Focus Company: " + focusOrgId + " targetOrgId: " + targetOrgId);
                }
            }
            return null;
        }

IPath接口:

    import java.util.List;

    import com.equilar.superconman.client.data.IConnObject;
    import com.fasterxml.jackson.annotation.JsonSubTypes;
    import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

    @JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="type")
    @JsonSubTypes({
        @Type(value=N2NPath.class, name="N2NPath")
    })
    public interface IPath extends IConnObject {

        Node getSource();

        void setSource(Node source);

        Node getTarget();

        void setTarget(Node target);

        int getLength();

        void setLength(int length);

        double getAverageWeight();

        void setAverageWeight(double averageWeight);

        List<Node> getPathNodes();

        void setPathNodes(List<Node> pathNodes);

        List<IConnection> getConnectionList();

        void setConnectionList(List<IConnection> connectionList);
    }

N2NPath类:

import java.util.List;

public class N2NPath implements IPath {

    private static final long serialVersionUID = -7139427288367077132L;

    private Node source;
    private Node target;
    private int length;
    private double averageWeight;
    private List<Node> pathNodes;
    private List<IConnection> connectionList;

    public N2NPath() {
    }

    public N2NPath(Node source, Node target) {
        this.source = source;
        this.target = target;
    }

    public Node getSource() {
        return source;
    }

    public void setSource(Node source) {
        this.source = source;
    }

    public Node getTarget() {
        return target;
    }

    public void setTarget(Node target) {
        this.target = target;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public double getAverageWeight() {
        return averageWeight;
    }

    public void setAverageWeight(double averageWeight) {
        this.averageWeight = averageWeight;
    }

    public List<Node> getPathNodes() {
        return pathNodes;
    }

    public void setPathNodes(List<Node> pathNodes) {
        this.pathNodes = pathNodes;
    }

    public List<IConnection> getConnectionList() {
        return connectionList;
    }

    public void setConnectionList(List<IConnection> connectionList) {
        this.connectionList = connectionList;
    }

    @Override
    public String toString() {
        return "N2NPath [source=" + source + ", target=" + target + ", length="
                + length + ", averageWeight=" + averageWeight + ", pathNodes="
                + pathNodes + ", connectionList=" + connectionList + "]";
    }

}

样品申请:

Url  : https://localhost:8443/bsp/org/orgorg-connections/63/3467
Mathod : POST
Body :  {
        "source": {
            "type": "Org",
            "id": 63
        },
        "target": {
            "type": "Org",
            "id": 3467
        },
        "length": 4,
        "averageWeight": 18.25,
        "pathNodes": [
            {
                "type": "Org",
                "id": 63
            },
            {
                "type": "Person",
                "id": 341680
            },
            {
                "type": "Person",
                "id": 147553
            },
            {
                "type": "Person",
                "id": 341518
            },
            {
                "type": "Org",
                "id": 3467
            }
        ],
        "connectionList": [
            {
                "type": "N2NConnection",
                "source": {
                    "type": "Org",
                    "id": 63
                },
                "target": {
                    "type": "Person",
                    "id": 341680
                },
                "weight": 0,
                "links": [
                    {
                        "type": "SEC_O2P",
                        "startDate": 1443682800000,
                        "endDate": 1443769200000,
                        "roleType": "BOARD_MEMBER",
                        "weight": 0,
                        "former": false,
                        "neo": true
                    }
                ]
            },
            {
                "type": "N2NConnection",
                "source": {
                    "type": "Person",
                    "id": 341680
                },
                "target": {
                    "type": "Person",
                    "id": 147553
                },
                "weight": 50,
                "links": [
                    {
                        "type": "SEC",
                        "orgId": 2617,
                        "sourceOrgId": 2617,
                        "targetOrgId": 2617,
                        "startDate": 1143187200000,
                        "endDate": 1171612800000,
                        "weight": 11,
                        "former": true,
                        "sourceRole": "EXECUTIVE",
                        "targetRole": "BOARD_MEMBER",
                        "show": true
                    },
                    {
                        "type": "SEC",
                        "orgId": 2617,
                        "sourceOrgId": 2617,
                        "targetOrgId": 2617,
                        "startDate": 1234166400000,
                        "endDate": 1325318400000,
                        "weight": 34,
                        "former": true,
                        "sourceRole": "EXECUTIVE",
                        "targetRole": "BOARD_MEMBER",
                        "show": true
                    },
                    {
                        "type": "SEC",
                        "orgId": 2617,
                        "sourceOrgId": 2617,
                        "targetOrgId": 2617,
                        "startDate": 1320130800000,
                        "endDate": 1333263600000,
                        "weight": 5,
                        "former": true,
                        "sourceRole": "EXECUTIVE",
                        "targetRole": "BOARD_MEMBER",
                        "show": true
                    }
                ]
            },
            {
                "type": "N2NConnection",
                "source": {
                    "type": "Person",
                    "id": 147553
                },
                "target": {
                    "type": "Person",
                    "id": 341518
                },
                "weight": 23,
                "links": [
                    {
                        "type": "SEC",
                        "orgId": 2617,
                        "sourceOrgId": 2617,
                        "targetOrgId": 2617,
                        "startDate": 1360569600000,
                        "endDate": 1420012800000,
                        "weight": 22,
                        "former": true,
                        "sourceRole": "BOARD_MEMBER",
                        "targetRole": "EXECUTIVE",
                        "show": true
                    },
                    {
                        "type": "SEC",
                        "orgId": 2617,
                        "sourceOrgId": 2617,
                        "targetOrgId": 2617,
                        "startDate": 1486540800000,
                        "endDate": 1489734000000,
                        "weight": 1,
                        "former": true,
                        "sourceRole": "BOARD_MEMBER",
                        "targetRole": "EXECUTIVE",
                        "show": true
                    }
                ]
            },
            {
                "type": "N2NConnection",
                "source": {
                    "type": "Person",
                    "id": 341518
                },
                "target": {
                    "type": "Org",
                    "id": 3467
                },
                "weight": 0,
                "links": [
                    {
                        "type": "SEC_O2P",
                        "startDate": 1490252400000,
                        "endDate": 1490252400000,
                        "roleType": "BOARD_MEMBER",
                        "weight": 0,
                        "former": false,
                        "neo": true
                    }
                ]
            }
        ]
    }

0 个答案:

没有答案