Java HTTP状态415 - TEXT_HTML的不支持的媒体类型

时间:2017-06-01 07:17:05

标签: java rest jersey

我使用jersey创建了一个RESTFul webservice,它应该在POST请求中使用纯文本并生成html响应我的Web服务类如下所示:

@Path("/calservices")
public class CalServices {
    @FormParam("num1")
    private String number1;
    @FormParam("num2")
    private String number2;

    @POST
    @Path("/cal")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_HTML)
    public String result(CalServices cal){
        int sum = Integer.parseInt(cal.number1)+Integer.parseInt(cal.number2);
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey ..Result is" +sum + "</body></h1>" + "</html> ";
    }
}

我的HTML表单看起来像这样

<body class="nav-md">
    <div class="right_col">
        <div class="container">
            <div class="panel panel-primary" style="width: 50%;text-align: center;margin:15% " >
                 <div class="panel-heading">Addition</div>
                <div class="panel-body">
                <form action="rest/calservices/cal" method="post">
                    <div class="form-group row">
                        <div class="col-md-4">Number First</div>
                        <div class="col-md-4"><input type="text" class="form-control" name="num1"></div>
                    </div>
                    <br/>
                    <div class="form-group row">
                        <div class="col-md-4">Number Second</div>
                        <div class="col-md-4"><input type="text" class="form-control" name="num2"></div>
                    </div>
                        <br>
                    <div class="form-group row center">
                        <div class="col-md-4"><input type="submit" value="add" class="btn btn-primary"></div>
                        <div class="col-md-4"><input type="reset" value="reset" class="btn btn-warning"></div>

                    </div>
                    </form>
                    <a href="rest/calservices/message">Click Here</a>
                </div>
            </div>
            </div>
        </div>

</body>

我的web.xml正在关注

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>com.vogella.jersey.first</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
   <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.my.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

0 个答案:

没有答案