Announcement Announcement Module
Collapse
No announcement yet.
REST Service confusion Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • REST Service confusion

    New to Spring and lost in general. Keep getting Content-Type: text/html;charset=utf-8 for my return even though I am trying to set it to XML. Read so many posts, blogs and examples I have confused myself. Need help.

    Excuses:
    • I have to return XML now but may need to switch to JSON later which is why there is a JSON object in the class
    • I removed a bunch of Debug and "If Null->Complain" code from the Controller so I hope it still makes sense
    • Did I mention how new I am

    Controller Class:

    Code:
        @RequestMapping(value = "/insertTestResults", method = RequestMethod.POST)
        @ResponseBody
        public PostReturn insertTestResults(@RequestParam("TestCaseId") String TestCaseId, @RequestParam("StartTime") String StartTime, @RequestParam("EndTime") String EndTime, @RequestParam("RunLength") String RunLength, @RequestParam("StatusId") String StatusId, @RequestParam("Status") String Status) throws ParserConfigurationException {
    		   JSONObject testRunID;
               PostReturn d = new PostReturn();
    
    		   try{
    
    
    			   // might be a pain but I am new to this and may need to debug a lot
    			   int TestCaseIdint = Integer.parseInt(TestCaseId);
                   int StatusIdint = Integer.parseInt(StatusId);
    
    			   EndTime = (EndTime != null)?EndTime:"1900-01-01  00:00:00";
    			   RunLength = (RunLength != null)?RunLength:"00:00:00";
    
    			   mySQLDatabaseConnection SQLConnect = new mySQLDatabaseConnection();
    
    			   testRunID = SQLConnect.insertTestResults(TestCaseIdint, StartTime, EndTime, RunLength, StatusIdint, Status,1);
    			   if (testRunID == null){
    				   log.msgError("No testRunID returned for insertTestResults");
                       d.setReturnElement("No_testRunID_returned");
                       d.setReturnText("-1");
    				   return d;
    			   }
    			   String testRunIdString = testRunID.getString("TestRunId");
                   log.msgInfo("Test Run Id: " + testRunIdString + " for Test Case " + TestCaseId);
    			   System.out.println("Return value; "+ testRunIdString);
                   d.setReturnElement("TestRunId");
                   d.setReturnText(testRunIdString);
    			   return d;
    		   }
    
    		   catch(Exception e){
    			   log.error(e.toString(), e);
                   d.setReturnElement("Exception_Thrown");
                   d.setReturnText(e.toString());
    			   return d;
    		   }
        }
    Object Class:

    Code:
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "",  propOrder = {
        "returnElement",
        "returnText"
    })
    @XmlRootElement(name = "PostReturn")
    public class PostReturn {
        private String returnElement;
        private String returnText;
    
        public void setReturnElement (String element){
            this.returnElement = element;
        }
    
        public void setReturnText (String text){
            this.returnText = text;
        }
    
        public String getReturnElement (){
            return this.returnElement;
        }
    
        public String getReturnText (){
            return this.returnText;
        }
    }
    XML File:
    HTML Code:
    	<bean
    		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
    		p:order="1">
    		<property name="mediaTypes">
    			<map>
    				<entry key="xml" value="application/xml"/>
    				<entry key="json" value="application/json" />
    			</map>
    		</property>
    		<property name="viewResolvers">
    			<list>
    				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    			</list>
    		</property>
    		<property name="defaultViews">
    			<list>
    				<bean class="org.springframework.web.servlet.view.xml.MarshallingView" p:modelKey="model">
                        <property name="marshaller">
                            <bean class="org.springframework.oxm.castor.CastorMarshaller" p:suppressXsiType="true"/>
                        </property> </bean>
    				<bean
    					class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    			</list>
    		</property>
    	</bean>

  • #2
    How do you know that you've really set it to return an XML?

    You can actually specify your controller to return both JSON and XML depending on the Accept header.

    Take a look at this example: http://krams915.blogspot.com/2011/02...vider-and.html

    Comment

    Working...
    X