I started programming from Java. The purpose was to make use of it in my work, Right now, I mainly make web services as a hobby. I was originally interested because I heard that Ruby on Rails is the mainstream in personal development and ventures. However, even though Java is also amateurish, learning another language is too expensive, and I thought it would be good if it could be shaped as a Web service.
So even with the Java framework, I was developing using Spring Boot, which is new and simple and quick to develop. From development to release with Spring Boot, I was able to do what I wanted to do. Still, I was curious about Ruby On Rails, but on the other hand, I was also attached to Spring Boot to some extent. I thought it was a little cool to use a framework different from other people, so I planned to continue using it as it is.
This was about 3 weeks ago.
I came up with the web service I wanted to create, and this time I started development with the goal of releasing it in a few weeks. However, I was addicted to Twitter login authentication. Of course, I've been having troubles because it didn't work well, so I've been researching various things, but the information on Twitter authentication using Spring Boot (I want to link Spring Social and Spring Security) is extremely small even if it is included in English. .. I tried none of them and it didn't work. I used it here for 2 days. (About 8 hours) To be honest, there were many parts where I could not understand the process of processing. I think there was also a way to look up the official reference from scratch and implement it on your own. I think it's okay to take that much time to implement the original function, but I had a strong desire to honestly copy and paste general parts such as Twitter authentication. When I looked it up in Ruby on Rails, I found many samples and the implementation was simple.
After that, I would like to research Rails and implement Web services individually with a sense of speed. In response to the request I clearly thought that Ruby On Rails was suitable. Learning costs are high, but if you look at it over a period of several months, it should be more efficient to switch. We decided to stop the service development and switch to it. I also switched from VScode to Atom.
I studied for about 2 weeks (40-50 hours). Read the Ruby version of dot install and the Ruby on Rails tutorial on the train and during lunch break It's like moving your hands after returning home to implement it.
Ruby on Rails Tutorial was skipped, and it took roughly 3 weeks to read through → implement → read through. From here, I plan to study the parts that I do not understand while actually creating the service.
Spring Boot vs Ruby on Rails Ruby on Rails was the best!
Insanely important. Especially since there is no one who can easily listen as a self-taught scholar The activity of Google teacher becomes a matter of life and death.
Spring Boot: Spring Boot does not have a lot of information on the Web. So I searched for a reference book, but I couldn't tell whether it was good or bad because there were about 4 options and few book reviews. I didn't have a reserve at a bookstore in my neighborhood, so I went to Shinjuku to buy it.
Ruby on Rails: I haven't paid 1 yen for learning this time. Ruby on Rails tutorials, online learning services for beginners It was a very easy-to-follow framework because you can learn a lot by searching. We have abundant samples, so we can expect to speed up future development.
Spring Boot: Java install, pass through, Maven install, pass through, It took about an hour to start using it. Beginners are at a frustrating level.
Ruby on Rails: I use a MAC, but it was so easy that I forgot how I made it. ※Forgot.
Spring Boot: Go to Spring Initializer and create a package. I still don't know what to put in the Group and Artiface items, and there are many selection items.
Ruby on Rails: Development can be started quickly with rails new from the console. It seems that the DB type can also be specified as an option.
Spring Boot: By default, you can't do anything in particular, so you need to decide the placement rules and create the folder yourself. At first, I didn't know the best practice, and I was wondering if this was all right, but I managed to do it with my own MVC.
Source folder made by Pochipochi:
Ruby on Rails: When you create a package with Rails new, folders for different purposes such as model, view, config, etc. are automatically created. Even if you don't care about the source code layout, it will be organized according to the default, which is helpful for beginners. You can omit the usual simple work of automatically generating it.
Automatically generated folder Source folder:
Spring Boot: It is managed by pom (Maven). The items described are like this.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Ruby on Rails: It is managed by a gem file. The items described are the same. Is it a little simple?
Gemfile
gem 'rails', '~> 5.2.0'
Spring Boot:
The controller handles URL and view mapping and passing values.
Since the mapping is described across multiple files, which URL is which view and controller
I often lose track of whether it is related. value =" / "
is the corresponding URL mav.setViewName ("index ")
Find and display the template (HTML file) named index. There are various other definition methods.
indexController.java
@Controller
public class indexContoroller{
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView showIndex (ModelAndView mav){
mav.setViewName("index");
return mav;
}
}
Ruby on Rails:
URL mapping is defined in routers.rb.
Only the URL mapping is cut out from the Spring Boot controller. It is highly readable because it can be listed. get'/ help', to:'static_pages # help'
has'/ help' as the URL
A class with processing to be executed by'static_pages # help'.
routers.rb
Rails.application.routes.draw do
get '/help', to: 'static_pages#help'
static_pages_controller.rb
class StaticPagesController < ApplicationController
def help;
#hogehoge
end
ends
Spring Boot: When you want to use DB, prepare a database, create a table, After that, you need to prepare several classes for DB connection. The operation is abstracted, but the number of steps to be able to write data is not small.
It's a bit long, but it describes the class prepared to operate the Member table.
Member.java
corresponds to the Member table
and receives the data.
You can customize the query with MemberRepository.java
.
The actual access is @Autowired MemberRepository repository;
in the class you want to use
Create a repository instance in and use it like repository.findById (id);
.
JpaRepository <Member, Integer>
will cause an error if you do not specify the type of the table's primary key.
MemberRepository.java
public interface MemberRepository extends JpaRepository<Member, Integer> {
public Member findByUsername(String username);
public boolean existsByUsername(String username);
}
Class corresponding to the table:
Member.java
Entity
@Table(name = "member")
public class Member implements UserDetails {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
// private Long userid;
private int userid;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String displyname;
public Member() {
super();
}
public String getDisplyname() {
return displyname;
}
public int getUserid() {
return userid;
}
public void setDisplyname(String displyname) {
this.displyname = displyname;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public String getUsername() {
return this.username;
}
public String getMail() {
return this.displyname;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
Ruby on Rails:
rails generate model" table name "content: text
When you execute the command like, the table and the class required for the operation are automatically generated.
You can suddenly read and write from the DB as shown below.
m = "table name".new
m.find_by()`
I have never connected directly to the DB so far. Rails almost forgets the existence of DB.
Spring Boot: I implemented the image upload function in Spring Boot, but it was quite difficult. Upload and validate in HTML and JS, After receiving it, put the path of the file in the DB and save the file locally. It was necessary to write the process from scratch.
Ruby on Rails: Regarding image upload, there is a gem called CarrierWave, It can be implemented with about 1/3 of the processing of Spring Boot. Spring has a lot of useful features, but Rails is better. I have a good impression when it comes to web development.
Spring Boot: In fact, Spring Boot is also bundled with Tomcat, so this is a draw. If it is the default port 80, it cannot be started locally on the MAC, so it is necessary to specify the port number and start it.
Ruby on Rails: The port number 3000 seems to be the default, so you don't have to do anything.
--Easy validation after receiving data --Simple template engine (Eruby vs Thymelefe) --Easy to componentize HTML --Easy to read JS and CSS (asset pipeline) --The flow from form to data writing is beautiful --Ajax Easy. JS Almost no need to write --The value is automatically passed from the controller to the view. --A view is automatically created when you create a controller
Rails has a lot of troublesome work in the background, I'm writing a program, but it feels like I'm playing a puzzle or a game. As for Spring Boot, the function can be implemented after going through various procedures, so the team was solid. I think that it is suitable for making large applications, but at the personal development level The current conclusion is that you don't need that tightness.