找不到媒体类型= application / xml的MessageBodyWriter,type = class com.webService.OrderService

时间:2015-05-19 06:42:30

标签: java eclipse web-services jersey

我正在使用RESTFUL API球衣。 Sever:Glassfish 4 IDE:Eclipse

我正在尝试访问我的资源:

http://localhost:8080/GigstartServices/GET/Users/1/Orders

网址的一部分:      http://localhost:8080/GigstartServices/GET/Users/1

工作得很好。但是当访问OrderService时会给出提到的异常。我可能出错了吗?

但是我得到一个例外:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class com.webService.OrderService, genericType=class com.webService.OrderService.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:471)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:333)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:323)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:227)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)

代码如下所示:  OrderService.java

package com.webService;

import java.sql.Connection;
import java.util.ArrayList;

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

import com.dao.Database;
import com.dao.OrderResource;
import com.dao.Resources;
import com.dto.Items;
import com.dto.Orders;
@Path("/")  
public class OrderService {
OrderService(){

}
    ArrayList<Orders> u = null;
    Database database= new Database();

        @GET
        @Produces(MediaType.APPLICATION_XML)
        public ArrayList<Orders> getOrders(@PathParam("userid") int userid) throws Exception
        {
            Connection connection = database.GetConnection();
            OrderResource ur= new OrderResource();
            u=ur.GetOrders(connection, userid);
         return u;
        }
}

Orders.java

@XmlRootElement
public class Orders {

    private int id;
    private ArrayList<Integer> items;
    private int userId;
    private int totalAmount;

public Orders(){

    }
private ArrayList<Integer> Items;
    public Orders(int id,ArrayList<Integer> items, int userId , int totalAmount){
        Items = new ArrayList<Integer>();
        this.id = id;
        this.Items = items;
        this.userId = userId;
        this.totalAmount = totalAmount;

    }
    /**
     * @return the Id
     */
    public int getId() {
        return id;
    }
    /**
     * set Id
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the ItemId
     */
    @XmlElementWrapper(name="Items")
    @XmlElement(name="Item")
    public ArrayList<Integer> getItemId() {
        return Items;
    }
    /**
     * @set name
     */
    public void setItem(ArrayList<Integer> items) {
        this.items = items;
    }
    /**
     * @return price
     */
    public int getUserId() {
        return userId;
    }
    /**
     * set price
     */
    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getTotalAmount(){
        return totalAmount;
    }

    public void setTotalAmount(int totalAmount){
        this.totalAmount = totalAmount;
    }
}

OrderResource.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.dto.Orders;

public class OrderResource {

    public ArrayList<Orders> GetOrders(Connection connection, int id) throws Exception
    {
        ArrayList<Orders> feedData = new ArrayList<Orders>();
        ArrayList<Integer> itemsId = new ArrayList<Integer>();
        try
        {
            PreparedStatement ps = connection.prepareStatement("SELECT o.id, o.userId, sum(I.price) as totalAmount FROM dbo.Orders o join dbo.Items I on o.itemId = I.id join dbo.Users u on o.UserId = u.Id where o.UserId = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                Orders o = new Orders();
                o.setId(rs.getInt("id"));
                ps = connection.prepareStatement("SELECT itemId FROM dbo.Orders o where o.UserId= ?");
                ps.setInt(1, id);
                ResultSet rs2 = ps.executeQuery();
                while(rs2.next()){
                itemsId.add(rs.getInt("itemId"));
                }
                o.setItem(itemsId);
                o.setUserId(rs.getInt("price"));
                o.setTotalAmount(rs.getInt("totalAmount"));
                feedData.add(o);
            }
            return feedData;
        }
        catch(Exception e)
        {
            throw e;
        }
    }

}

UserServcie.java

package com.webService;
import java.sql.Connection;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlTransient;

import com.dao.OrderResource;
import com.dao.Resources;
import com.dao.Database;
import com.dto.Users;

@Path("/Users") 
    public class UserService {
    ArrayList<Users> u = null;
    Database database= new Database();

        @GET
        @Produces(MediaType.APPLICATION_XML)
        public ArrayList<Users> getUsers() throws Exception
        {
            Connection connection = database.GetConnection();
            Resources ur= new Resources();
            u=ur.GetUsers(connection);
         return u;
        }
        @Path("/{id}")
        @GET
        @Produces(MediaType.APPLICATION_XML)
        public ArrayList<Users> getUsers(@PathParam("id") int id) throws Exception
        {
            Connection connection = database.GetConnection();
            Resources ur= new Resources();
            u=ur.GetUsers(connection, id);
         return u;
        }

        @XmlTransient
        @GET
        @Path("/{userid}/Orders")
        @Produces(MediaType.APPLICATION_XML)
        public OrderService getOrders(@PathParam("userid") int userid) throws Exception
        {
            return new OrderService();
        }
    }

0 个答案:

没有答案