Чтение XML из почтового запроса REST с использованием Springboot - PullRequest
1 голос
/ 03 июля 2019

Я пытаюсь использовать XML, полученный через веб-сервис REST с помощью Springboot, я новичок в этом, так что надеюсь, что я совершаю довольно тривиальную ошибку.

Я думаю, что XML может быть плохо структурирован, но у меня есть следующее ....

<ResultDetailsRequestModel>
<tournament>Premier League</tournament>
<fixture_date>2018-08-10</fixture_date>
<description>result</description>
<home_team>
    <team_name>Man Utd</team_name>
    <score>2</score>
    <ht_score>1</ht_score>
    <possession>46.3</possession>
    <shots_on_target>6</shots_on_target>
    <shots_off_target>1</shots_off_target>
    <corners>2</corners>
    <player gk="true" position="G" number="1">D de Gea</player>
    <player position="D" number="2">V Lindelof</player>
</home_team>
</ResultDetailsRequestModel>

Но сведения об игроке не читаются, я получаю следующее, когда делаю toString для полученных данных.

ResultDetailsRequestModel(tournament=Premier League, fixture_date=Fri Aug 10 01:00:00 BST 2018, description=result, home_team=TeamResultXML(team_name=Man Utd, score=2, ht_score=1, possession=46.3, shots_on_target=6, shots_off_target=1, corners=2, players=null))

Я могу получить информацию, когда окружаю теги игрока

<players> ... </players> 

но изменение XML имеет большое влияние в других местах, так что надеемся исправить с XML, как есть.

Мой код выглядит следующим образом ...

@RestController
@RequestMapping("results") // http://localhost:8888/results
public class ResultsController {

    @PostMapping(
            consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
            produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }
    )
    public String createBets(@RequestBody ResultDetailsRequestModel resultDetails) {
        return "Received details: " + resultDetails.toString();
    }
}
@Data
@XmlRootElement(name="ResultDetailsRequestModel")
public class ResultDetailsRequestModel {
    private String tournament;
    private Date fixture_date;
    private String description;
    private TeamResultXML home_team;

}
@Data
@XmlRootElement(name="home_team")
public class TeamResultXML implements Serializable {
    private String team_name;
    private int score;
    private int ht_score;
    private double possession;
    private int shots_on_target;
    private int shots_off_target;
    private int corners;
    @XmlElement(name="player")
    private List<PlayerXML> players;

}
@Data
@XmlRootElement(name="player")
public class PlayerXML implements Serializable {
    @XmlAttribute(name="gk")
    private boolean goalkeeper;

    @XmlAttribute(name="position")
    private String position;

    @XmlAttribute(name="number")
    private int number;

    @XmlAttribute(name="sub")
    private String sub;

    @XmlValue
    private String value;
}

Данные, которые я получаю ....

ResultDetailsRequestModel(tournament=Premier League, fixture_date=Fri Aug 10 01:00:00 BST 2018, description=result, home_team=TeamResultXML(team_name=Man Utd, score=2, ht_score=1, possession=46.3, shots_on_target=6, shots_off_target=1, corners=2, players=null))

Аннотация @Data - это проект Lombok, поэтому создаются геттеры, сеттеры и т. Д.

1 Ответ

1 голос
/ 03 июля 2019

Когда я пробую ваш код, я получаю ошибки:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 7 counts of IllegalAnnotationExceptions
If a class has @XmlElement property, it cannot have @XmlValue property.
    this problem is related to the following location:
        at private java.lang.String PlayerXML.value
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at public java.lang.String PlayerXML.getValue()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "players"
    this problem is related to the following location:
        at public java.util.List TeamResultXML.getPlayers()
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "goalkeeper"
    this problem is related to the following location:
        at public boolean PlayerXML.isGoalkeeper()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private boolean PlayerXML.goalkeeper
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "number"
    this problem is related to the following location:
        at public int PlayerXML.getNumber()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private int PlayerXML.number
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "position"
    this problem is related to the following location:
        at public java.lang.String PlayerXML.getPosition()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private java.lang.String PlayerXML.position
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "sub"
    this problem is related to the following location:
        at public java.lang.String PlayerXML.getSub()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private java.lang.String PlayerXML.sub
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
Class has two properties of the same name "value"
    this problem is related to the following location:
        at public java.lang.String PlayerXML.getValue()
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel
    this problem is related to the following location:
        at private java.lang.String PlayerXML.value
        at PlayerXML
        at private java.util.List TeamResultXML.players
        at TeamResultXML
        at public TeamResultXML ResultDetailsRequestModel.getHome_team()
        at ResultDetailsRequestModel

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at Test8.main(Test8.java:17)

Когда я исправляю это, добавляя @XmlAccessorType(XmlAccessType.FIELD) ко всем 3 классам, я получаю ожидаемый результат:

ResultDetailsRequestModel [tournament=Premier League, fixture_date=Fri Aug 10 00:00:00 EDT 2018, description=result, home_team=TeamResultXML [team_name=Man Utd, score=2, ht_score=1, possession=46.3, shots_on_target=6, shots_off_target=1, corners=2, players=[PlayerXML [goalkeeper=true, position=G, number=1, sub=null, value=D de Gea], PlayerXML [goalkeeper=false, position=D, number=2, sub=null, value=V Lindelof]]]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...