简体   繁体   English

RESTFUL Web服务-媒体类型= application / xml

[英]RESTFUL web service - media type=application/xml

I'm currently writing a RESTFUL web service and trying to return integer to the web service. 我目前正在编写RESTFUL Web服务,并尝试将整数返回到Web服务。

I encounter 500 Internal Server Error from the browser and when I check the Tomcat Log, the above error occurred. 我在浏览器中遇到500 Internal Server Error,当我检查Tomcat Log时,发生了上述错误。

12-Nov-2018 09:47:12.547 SEVERE [http-nio-8080-exec-52] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/xml, type=class java.lang.Integer, genericType=int.

My code : 我的代码:

@POST
    @Path("/post")

    @Produces(MediaType.APPLICATION_XML)
    public static int adaptiveAuth(){ 
        int message=1;
        return message; 
    }

If I replace the function with String, it wont give any error. 如果我将函数替换为String,它将不会出现任何错误。

@POST
    @Path("/post")

    @Produces(MediaType.APPLICATION_XML)
    public static String adaptiveAuth(){ 
        String message="POST STRING";
        return message; 
    }

Result : POST STRING 结果:STRING后

Is there any limitation on RESTFUL regarding MediaType.APPLICATION_XML ? 关于MediaType.APPLICATION_XML的RESTFUL是否有任何限制?

Thank you 谢谢

Hello try to use Response object from jax-rs specification 您好尝试使用jax-rs规范中的Response对象

https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.html https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.html

It's better to return response object which gives you flexibility to define status , body etc. 最好返回响应对象,它使您可以灵活地定义状态,正文等。

Also you can see existing answer on this topic: 您还可以查看有关此主题的现有答案:

Returning an Integer from RESTful web services method in java 从Java中的RESTful Web服务方法返回整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM