415 Unsupported JAXB media type in Spring

My headers for POST setupMain post setup

I'm a little stuck here. I am trying to POST a small XML fragment from a firefox poster.

<IntellexEvent> <RuleName>a rule name</RuleName> </IntellexEvent> 

Simple enough, now my class for IntellexEvent is -

 @XmlRootElement(name = "IntellexEvent") public class IntellexEvent { // @XmlElement(name = "RuleName") private String RuleName; public String getRuleName() { return RuleName; } public void setRuleName(String RuleName) { this.RuleName = RuleName; } 

}

My controller ...

 @Controller @RequestMapping("/cace/**") public class CaceController { @Autowired IUserService userService; public CaceController() { } @RequestMapping(value = "/cace/postXML", method = RequestMethod.POST) public Result postXML(@RequestBody String intellexEvent) throws Exception { String temp = intellexEvent; Result result = new Result(); result.setStatusCode(200); result.setSuccess(true); return result; } 

}

- EDITED - So, here I have @RequestBody as a string. I wanted this to be automatically connected to IntellexEvent ... As a string, I can hit my backend on POST, when I change String to IntellexEvent, I get 415 error.

I just want to hit my backend, I tried GET and I just hit (I didn't include them in my controller here), what am I missing here? In spring -mvc-servlet.xml, I defined the jaxb2 marshaller. If you need more information, just ask, thanks in advance guys!

+4
source share
1 answer

You are probably missing the Content-Type header, which should be application/xml , this is for Spring -MVC to know that you are sending xml. Also, if you expect the response to be xml, you annotate your postXML method with @ResponseBody and have an Accept application/xml header.

PostXML should look like this:

 @RequestMapping(value = "/cace/postXML", method = RequestMethod.POST) public @ResponseBody Result postXML(@RequestBody IntellexEvent intellexEvent) throws Exception 

Another thing I noticed is that you need to uncomment @XmlElement(name = "RuleName") with the ruleName field, otherwise the tag will be <ruleName/>

+1
source

Source: https://habr.com/ru/post/1440818/


All Articles