使用Java和mysql开发Web服务 [英] developing a webservice using java and mysql

查看:303
本文介绍了使用Java和mysql开发Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉websrvices和mysql ..i,它遵循 http://www .vogella.com/articles/REST/article.html 教程,并使用JAX-RS参考实现Jersey开发了Java RESTful Web服务.

I am not familiar with websrvices and mysql ..i followed this http://www.vogella.com/articles/REST/article.html tutorial and developed a RESTful web service in Java with the JAX-RS reference implementation Jersey.

我想使用eclipse创建一个websrevice,它从mysql数据库中选择数据并以xml格式显示结果.

I want to create an websrevice using eclipse which select the data from mysql database and display the result in xml format.

我有很多使用PHP和mysql开发Web服务的示例,但是我想用Java开发.

I got lots of sample developing webservices with PHP and mysql but i want to develop in java.

请向我建议一些有关此或想法的好的教程/链接,可能会对我有所帮助.我想在我的Android应用程序中使用该Web服务.

Please suggest me some good tutorial/links on this or idea which may help me. i want to use that webservice in my android application.

在上面的示例中,我无法在哪里放置连接字符串来建立MySQL数据库和Java文件之间的连接.

In the above example ,i am not getting where to put the connection string to establish the connection between MySQL database and java files.

这里是TodoResource.java:

Here is the TodoResource.java :

package de.vogella.jersey.jaxb;

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

import de.vogella.jersey.jaxb.model.Todo;

@Path("/todo")
public class TodoResource {
    // This method is called if XMLis request


    @GET
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    //@Produces( { MediaType.TEXT_XML })
    public Todo getXML() {
        Todo todo = new Todo();


        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        todo.setMy_id(1);
        return todo;



    }

    // This can be used to test the integration with the browser
    @GET
    @Produces( { MediaType.TEXT_XML })
    public Todo getHTML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

}

推荐答案

您链接到的示例实际上并未使用数据库,而是使用内存中的 Todo 来提供数据.在第8节中,作者声明

The example you linked to does not actually make use of a database but uses in-memory Todo's to provide the data. In section 8 the author states

创建以下数据模型和一个Singleton,用作 模型的数据提供者.我们使用基于 枚举.

Create the following data model and a Singleton which serves as the data provider for the model. We use the implementation based on an enumeration.

数据模型是 Todo 类.

数据提供者是 TodoDao 枚举. TodoDao 的目的实质上是将 Todo 的内容存储在内存中.换句话说,它执行原本由数据库完成的功能.

The data provider is the TodoDao enum. The purpose of TodoDao, is essentially to store Todo's in memory. In other words, it performs the function that would otherwise be done by a database.

因此,需要做的是:

  1. 用数据库替换 TodoDao .
  2. Todo 映射到数据库中的表.
    • 要将Java对象连接到数据库,可以使用对象关系映射器(ORM),可以通过使用Java Persistence API(JPA)来实现.
    • 因此,要将 Todo 映射到数据库表,需要使用JPA批注对其进行批注,从而创建JPA实体.
  1. Replace TodoDao with a database.
  2. Map Todo to a table in the database.
    • To connect Java objects to a database, you can use an Object Relational Mapper (ORM), which can be achieved by using the Java Persistence API (JPA).
    • Therefore, to map Todo to a database table, it needs to be annotated with JPA annotations, thus creating a JPA Entity.

看看对 REST/JSON Web服务Java EE框架的公认答案,它应该可以阐明什么需要做的.第1部分介绍如何创建数据库,而第2部分介绍如何创建和注释JPA实体(第3部分-xml或json的JAXB绑定,第4部分-RESTFul服务,第5部分-客户端).

Have a look the accepted answer to REST/JSON Web Services Java EE Framework, it should shed some light on what needs to be done. Part 1 covers creating a database, while Part 2 covers creating and annotating JPA entities (Part 3 - JAXB Bindings for xml or json, Part 4 - The RESTFul Service, Part 5 - The Client).

如果仍然遇到困难,请查看我为

If difficulties are still encountered, have a look at the answer I posted for Need to write a RESTful JSON service in Java, that should be suitable for someone who wants more nitty-gritty to, as a starting point, connect to a single table in a database and create a RESTful Web Service with JSON/XML representations using the following.

  • IDE:面向Jave EE开发人员(Kepler)的Eclipse IDE,内置了Maven
  • 数据库:MySQL(也使用MySQL Workbench)
  • 应用程序服务器:GlassFish 4.0
  • Java EE 7(JAX-RS,JPA,JAXB等)
  • 任何要测试的REST客户端:(例如邮递员)

这篇关于使用Java和mysql开发Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆