Why writer for media type application/json missing

前端 未结 3 982
无人共我
无人共我 2021-01-14 04:07

Basically I have a restful service (post) that consumes(application/json) and produces (application/json). The single param for this service is an

相关标签:
3条回答
  • 2021-01-14 04:38

    Raman is correct. Jettison is a valid option. You can also use Jackson. If you are using maven, it is as simple as including the following dependency in you pom:

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>2.3.2.Final</version>
        </dependency>
    

    At which point you should have no problem writing code such as:

        SomeBean query = new SomeBean("args")
        request.body("application/json", query);
        ClientResponse response = request.post();
    
    0 讨论(0)
  • 2021-01-14 04:39

    actually I had the same problem, I did solve it by adding jettison provider for application/json mime type. I don't know whether resteasy 1.1 containts jettison provider but version 1.2 does. Also if you are using jdk 1.6 you must exclude javax.xml.stream:stax-api jar file, otherwise you will have a problem.

    Here is the example:

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    
    @XmlRootElement(name="account")
    public class Account {
    
        private Long id;
        private String accountNo;
    
    
        public Account(){}
        public Account(String no)   {
            accountNo=no;
        }
    
    
        @Id
        @XmlElement
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
    
        @XmlElement
        public String getAccountNo() {
            return accountNo;
        }
        public void setAccountNo(String a) {
            accountNo = a;
        }
    
    }
    

    and JAXB class:

    import java.util.ArrayList;
    import java.util.List;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    
    
        @Path("/account")
        public class AccountService {
    
    
            @GET
            @Path("/{accountNo}")
            @Produces("application/json")
            public Account getAccount(@PathParam("accountNo") String accountNo) {
                       return new Account(accountNo);
            }
    
        }
    

    That's all, have a nice day!

    0 讨论(0)
  • 2021-01-14 04:42

    Add below to the Resource class or the method causing the exception

    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    
    0 讨论(0)
提交回复
热议问题