java mysql开发,使用Java和mysql开发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.

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

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

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.

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

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;

}

}

解决方案

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

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.

The data model is the Todo class.

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.

What therefore needs to be done, is to:

Replace TodoDao with a database.

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.

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: Eclipse IDE for Jave EE Developers (Kepler), comes with Maven built in

Database: MySQL (also makes use of MySQL Workbench)

Application Server: GlassFish 4.0

Java EE 7 (JAX-RS, JPA, JAXB, etc)

Any REST Client for testing: (eg Postman)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值