javax.xml.bind.UnmarshalException:意外元素(uri:"",local:" html")

时间:2017-02-21 03:38:24

标签: spring web-services rest jaxb unmarshalling

当我尝试从我的Web服务获取对象时,我收到以下异常:

 javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"html"). Expected elements are <{}user>

用户类注释为@XmlRootElement,通过浏览器访问Web服务显示用户的xml表示

客户端代码(我使用MediaType.TEXT_HTML因为MediaType.APPLICATION_XML导致RestClientException):

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);
User x = restTemplate.getForObject("http://localhost:8080/Online_Shopping/dispatcher/rest/hello",User.class);

网络服务代码:

@Controller
@RequestMapping(value = "/rest")
public class RESTController {

    @RequestMapping(value = "/hello", 
    method = RequestMethod.GET, 
    produces = MediaType.APPLICATION_XML_VALUE)
    public @ResponseBody User getUser() {
      return new  User("paymon","123",true); 
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,由于安全拦截器,我无法访问Web服务。 <intercept-url pattern="/dispatcher/rest/**" access="hasRole('ROLE_ADMIN')"/>在评论出拦截器后,我的网络服务正常运行。唯一的问题是我想知道如何保护我的Web服务通过Java代码访问它。