java 休息 | @Produces 和 @Consumes :为什么他们都没有被调用为相同的 MIME 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17148529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:09:16  来源:igfitidea点击:

Rest | @Produces and @Consumes : why dont they both get called for same MIME-type

javarestjersey

提问by arthur

a newbie in JAX-REST (jersey 1.8 impl)

JAX-REST 的新手(jersey 1.8 impl)

I have a class for Resource "/hello"

我有一个资源类“/hello”

package com.lbs.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Test {


    //-- produces MIME type text/plain
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public  String thankYouTxt(){
        System.out.println("thankYouTxt");

        return "thankYouTxt\n";
    }

    //-- consumes MIME type text/plain
    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    public String thankYouInputTxt(){
        System.out.println("thankYouInputTxt");
        return "thankYouInputTxt";

    }


    //-- produces MIME type text/html
    @GET
    @Produces(MediaType.TEXT_HTML)
    public  String thankYouHTML(){
        System.out.println("thankYouHTML"); 
        return "thankYouTxtHTML";
    }


    //-- consumes MIME type text/html
    @GET
    @Consumes(MediaType.TEXT_HTML)
    public  void thankYouInputHTML(){
        System.out.println("thankYouInputHTML");
        //return "thankYouInputHTML";
    }



    //-- produces MIME type text/xml 
    @GET
    @Produces(MediaType.TEXT_XML)
    public  String thankYouXML(){
        System.out.println("thankYouXml");
        return "<?xml version=\"1.0\"?> <message>thankYouTxt</message>";
    }
    //-- consumes MIME type text/xml    
    @GET
    @Consumes(MediaType.TEXT_XML)
    public String thankYouInputXML(){
        System.out.println("thankYouInputXML");
        return "thankYouInputXML";
    }



}

when I sent a request with a header Content-Type : text/html, I would expect both the @Producesand @Consumesannotated methods thankYouHTML()and thankYouInputHTML()to be called.

当我发送的请求有一个头Content-Type : text/html,我希望无论是@Produces@Consumes注解的方法thankYouHTML()thankYouInputHTML()被调用。

but only the @Produces thankYouHTML()method get called? why? why is not the @Consumesannotated method thankYouHInputTML()also called?

但只有@Produces thankYouHTML()方法被调用?为什么?为什么不也调用带@Consumes注释的方法thankYouHInputTML()

回答by pWoz

You should remember that:

你应该记住:

  1. Only one method is executed for a single request. So it is impossible to execute two methods (or more) in single request;
  2. JAX-RS runtime decides which one method should be executed according to request header values sent to to the server.
  1. 对于单个请求只执行一种方法。所以不可能在单个请求中执行两个(或多个)方法;
  2. JAX-RS 运行时根据发送到服务器的请求头值决定应该执行哪一种方法。

JAX-RSruntime tries to match:

JAX-RS运行时尝试匹配:

  • http method (GET,POST, ...) with proper annotation (@GET, @POST,...);

  • request path ('/api/something') with the proper @Pathannotation;

  • http content-typeheader (link) with proper @Consumesannotation;

  • http acceptheader with propper @Producesannotation;

  • http 方法 ( GET, POST, ...) 带有适当的注释 ( @GET, @POST,...);

  • 请求路径 ( '/api/something') 带有正确的@Path注释;

  • 带有正确注释的httpcontent-type标头(链接@Consumes

  • accept带有正确@Produces注释的http标头;

So (for example) @Producesannotation does not denote that annotated method produces something. It denotes that method will be executed when matching accept headerwill be contained in request.

所以(例如)@Produces注解并不表示注解的方法会产生一些东西。它表示当匹配accept header将包含在请求中时将执行该方法。

If You need more information abou RestFull webservices i advice You to read these resources:

如果您需要有关 RestFull 网络服务的更多信息,我建议您阅读以下资源: