使用REST

时间:2018-02-28 11:42:36

标签: java xml rest spring-mvc jaxb

我无法更改Spring MVC Controller返回的XML中的标记名称,Srpring将XML标记作为Java对象名称返回,而不是按预期方式返回。我已经使用@XmlElement根据需要设置标签名称但不起作用。

我尝试在属性而不是getter中添加XmlElement,没有用,我已经找了很多解决方案,但没有一个工作,我已经为此工作添加了所有必要的JAXB注释,请尽可能帮助我对此没有进一步的进展。

控制器:

@RequestMapping(value = "/customerAttributes/{number}", method = 
RequestMethod.GET, produces = "application/xml")
@ResponseBody
public Customer getCustInfo(@PathVariable String number) {

  Address add = new Address();
  Customer cust = new Customer();

  add.setHouse(number);
  add.setStreet("Street");
  cust.setName("Ankush");
  cust.setAdd(add);
  return cust;
  }

客户:

package com.fti.di.customer.domain;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"Name","add"})
@XmlRootElement(name = "DI_Customer")
public class Customer {

private String name;
private Address add;

@XmlElement(name = "DI_Name", required = true)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@XmlElement(name = "DI_Add", required = true)
public Address getAdd() {
    return add;
}
public void setAdd(Address add) {
    this.add = add;
}   
}

地址:

package com.fti.di.customer.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "",propOrder = {"Street","House"})
public class Address {

private String street;
private String house;

@XmlElement(name = "DI_Street")
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
@XmlElement(name = "DI_House")
public String getHouse() {
    return house;
}
public void setHouse(String house) {
    this.house = house;
}   
}

结果:

<Customer>
  <add>
      <street>Street</street>
      <house>3012</house>
  </add>
  <name>Ankush</name>
</Customer>

预期:

<DI_Customer>
  <DI_Add>
      <DI_Street>Street</DI_Street>
      <DI_House>3012</DI_House>
  </DI_Add>
  <DI_Name>Ankush</DI_Name>
</DI_Customer>

3 个答案:

答案 0 :(得分:2)

这个解决方案适用于我:

this.process[id].on('started', this.onStartedProcess.bind(this, 5));

that.emit('started', {startTime:time, instance: that});

    onStartedProcess(info, otherParameter) {
    console.log(otherParameter); // I was expecting the get the 5 here
    }

请注意区分大小写的onStartedProcess

答案 1 :(得分:0)

Victor的回答将解决您的问题。他所做的是他取消了 来自Customer类的@XmlAccessorType(XmlAccessType.FIELD)注释。

您也可以在班级客户:

  • 保留@XmlAccessorType(XmlAccessType.FIELD)并将@XmlElement注释移到字段上方(而不是吸气剂)
  • 将您的注释保持原样(即吸气剂)并更改为@XmlAccessorType(XmlAccessType.PROPERTY)

可以找到他们之间差异的解释以及在实践中展示它们的示例here

答案 2 :(得分:0)

from bs4 import BeautifulSoup
import requests
from contextlib import closing
from selenium.webdriver import Chrome # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import selenium 
import os
import re
import csv

import sys

pPortalBS = requests.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
pPortal = BeautifulSoup(pPortalBS.content,"html5lib")

#print pPortal

chromedriver = "C:/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
#driver = webdriver.Chrome(chromedriver)
#12,18,30,39,48,76,78
i=0
logged_in_flag = 0
with closing(Chrome(chromedriver)) as driver:
    while(1):
        try:
            driver.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
            '''
            try:
                element = WebDriverWait(driver, 20).until(
                EC.presence_of_element_located((By.ID, "hrdSelection"))
                )
            finally:
                driver.quit()
            '''
            #button = driver.find_element_by_id('idp')
            page_source = driver.page_source
            #page_source
            button = driver.find_elements_by_xpath(".//*/div[@class='idpDescription float']")
            print button
            button[1].click()
            # wait for the page to load
            element = WebDriverWait(driver, 10).until(
            EC.invisibility_of_element_located((By.ID, "hrdSelection"))
            )
            # store it to string variable
            page_source = driver.page_source

            #logging in

            if(logged_in_flag ==0):
                try:
                    usernameInput = driver.find_element_by_name("UserName")
                    usernameInput.send_keys("shumayan@publicisgroupe.net")
                    passwordInput = driver.find_element_by_id("passwordInput")
                    passwordInput.send_keys("Xendering@678")

                    driver.find_element_by_id("submitButton").click()
                    logged_in_flag = 1
                except:
                    logged_in_flag = 1



            #people portal home

            with closing(open("Go West Resource List.csv", "r")) as ifile:

                with closing(open("Go West Resource List Out Detailed Missing.csv", "a")) as ofile:

                    fileWriter = csv.writer(ofile)
                    reader = csv.reader(ifile)
                    rownum = 0
                    print reader

                    for row in reader:
                        # Save header row.
                        #print rownum
                        newRow = row
                        if rownum == 0:
                            header = row
                        elif rownum in range(1,i+1):
                            rownum = rownum +1
                            continue;
                        else:
                            colnum = 0
                            for col in row:
                                if(colnum == 1):
                                    if int(col) in [107590,21471,54226,130525,127212,125423,120988,48426,46183,59615,89696,88052,120465,59615,56277,119013,39460,46183,22920,79590,43848,25908,52556,60466,107439,100470,107439,105611,78666,78666,82833,59615,101430,59615,117872,82833,20332,117872,22920,23703,84858,59615,59615,63648,185,105611,121585,120043,107439,79564,22116,46183,91066,51892,46183,52591,77173,77762,91238,1018,129661,1018,113662,49410,24560,127848,46982,46183,133378,46982,57335,46982,46982,46982,52591,46982,77173,46982,46982,46982,46982,46982,46982,46982,46982,90166,50615,46183,46183,21702,115801,46183,23703,23703,219,49410,132,1127,339,46183,24560,1607,23703,665,78666,117872,43544,21235,1127,110479,99,219,46738,82002,182,23531,62022,128329,21668,62924,22738,46183,219,20923,54262,21702,118739,178,49220,23703,99,61075,660,77762,117872,21702,70423,28588,21795,101430,46183,83720,70927,33632,82334,41456,50892,25908,109358,117362,32474,90166,24560,31523,48632,90166,88052,49410,81322,38700,59615]:
                                    #1049
                                        print "ok"
                                    else:
                                        continue

                                    #try:
                                    print (header[colnum], col)
                                    #col = 133916
                                    element = WebDriverWait(driver, 20).until(
                                    EC.visibility_of_element_located((By.ID, "txtSearchPeople"))
                                    )
                                    searchbox = driver.find_element_by_id("txtSearchPeople")
                                    #oidInput = driver.find_element_by_id("passwordInput")
                                    searchbox.send_keys(col)
                                    searchbutton = driver.find_element_by_id("lbtnGoPeople")
                                    searchbutton.click()
                                    person = driver.find_element_by_id("NameFieldLink").click()
                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "dvMyInformationContent"))
                                    )

                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "dvAssignmentDetails"))
                                    )

                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "tblPhoneNumberAddEdit"))
                                    )
                                    page_source = driver.page_source
                                    pPortal = BeautifulSoup(page_source, "html5lib")
                                    informationWindow = pPortal.find("div",{"id":"dvMyInformationContent"})
                                    #print informationWindow.text
                                    informationWindowRows = informationWindow.findAll("div",{"class":"row"})
                                    startDate = ""
                                    IM=""
                                    for informationWindowRow in informationWindowRows:
                                        informationWindowRowData = informationWindowRow.findAll("div")
                                    #print informationWindowRowData
                                        foundDateFlag = 0
                                        foundIMflag = 0
                                        foundEmailFlag = 0
                                        IM_visited = 0
                                        SD_visited = 0
                                        EM_visited = 0
                                        for data in informationWindowRowData:
                                            #print data.text
                                            if(data.text == "IM"):
                                                foundIMflag = 1
                                                continue;
                                            if(foundIMflag == 1 and IM_visited == 0):   
                                                IM = data.text
                                                newRow.append(IM)
                                                IM_visited = 1



                                            if(data.text == "Start Date"):
                                                foundDateFlag = 1
                                                continue;
                                            if(foundDateFlag == 1 and SD_visited == 0): 
                                                startDate = data.text
                                                newRow.append(startDate)
                                                SD_visited = 1

                                            if(data.text == "Email"):
                                                foundEmailFlag = 1
                                                continue;
                                            if(foundEmailFlag == 1 and EM_visited == 0):    
                                                email = data.text
                                                newRow.append(email)
                                                EM_visited = 1



                                        if(foundDateFlag ==1 and foundIMflag == 1 and foundEmailflag == 1):
                                            break;


                                    titleBS = pPortal.find("div",{"class":"myprofile-name"})    
                                    titleSpans = titleBS.findAll("span")
                                    title = titleSpans[-1].text
                                    newRow.append(title)

                                    home_location = ""
                                    current_location = ""
                                    employee_category = ""
                                    comp_approver = ""
                                    comp_communicator = ""
                                    people_manager = ""

                                    assignmentDetails = pPortal.find("div",{"id":"dvAssignmentDetails"})        
                                    detailsDivs = assignmentDetails.findAll("div",{"class":"table tblBorder"})
                                    for detailDiv in detailsDivs:
                                        rows = detailDiv.findAll("div",{"class":"row"})
                                        for row in rows:
                                            divs = row.findAll("div")

                                            if(divs[0].text == "Home Location"):
                                                home_location = divs[1].text
                                                print home_location
                                            if(divs[0].text == "Current Location"):
                                                current_location = divs[1].text

                                            if(divs[0].text == "Employee Category"):
                                                employee_category = divs[1].text

                                            if(divs[0].text == "Comp Approver"):

                                                comp_approver = divs[1].text
                                            if(divs[0].text == "Comp Communicator"):
                                                comp_communicator = divs[1].text

                                            if(divs[0].text == "People Manager"):
                                                people_manager = divs[1].text

                                    newRow.append(home_location)
                                    newRow.append(current_location)
                                    newRow.append(employee_category)
                                    newRow.append(people_manager)
                                    newRow.append(comp_approver)
                                    newRow.append(comp_communicator)

                                    phoneWindow = pPortal.find("table",{"id":"tblPhoneNumberAddEdit"})      
                                    details = phoneWindow.find("tbody",{})

                                    trs = details.findAll("tr")
                                    phoneNos = []
                                    for tr in trs:
                                        td = tr.findAll("td")
                                        phoneNos.append(str(td[0].text))

                                    newRow.append(phoneNos)
                                    fileWriter.writerow(newRow)
                                    i=i+1

                                    print "success "+str(i)
                                    #except:
                                    #   continue;
                                colnum = colnum + 1
                        rownum = rownum + 1

                    print "end"
            break;
        except selenium.common.exceptions.NoSuchElementException, e:
            print "fail "+str(i)
            print str(e)

            i=i+1
            continue
        except Exception, e:
            print "fail "+str(i)
            print str(e)
            i=i+1
            logged_in_flag = 1
            continue