首页 技术 正文
技术 2022年11月19日
0 收藏 763 点赞 2,383 浏览 6826 个字

1.下载Google浏览器并安装插件

转载:

http://chromecj.com/web-development/2015-03/401/download.html

打开Google浏览器-》点击右上角Spring—-Spring Boot Rest的使用方法-》更多工具-》扩展程序

然后把Spring—-Spring Boot Rest的使用方法文件复制进来

打开右上角Spring—-Spring Boot Rest的使用方法

完成

2.RestMain.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/**
* 测试RestFul
*
*/
@SpringBootApplication //等同于 @Configuration @EnableAutoConfiguration @ComponentScan
public class RestMain
{
public static void main( String[] args )
{
SpringApplication.run(RestMain.class, args);
}
}

3.BeanConfig.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration
@ComponentScan(basePackages="com.maven") //这是扫描的包的主路径
@EnableAspectJAutoProxy //AOP代理自动配置
public class BeanConfig {
// @Bean
// public AppService service(){
// return new AppService();
// }// @Bean
// public User user(){
// return new User();
// }// @Bean
// public ContactBook contactBook(){
// return new ContactBook();
// }// @Bean
// public AspectConcern aspectConcern(){
// return new AspectConcern();
// }
}

4.WebConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
/**
* 这里的@bean()需要赋值一个名称,否则会抛出没有创建这个bean的异常
*
*/
@Bean("defaultServletHandlerMapping")
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}}

5.LoginController.java

import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import com.maven.demo.model.User;@Controller
public class LoginController {
@RequestMapping(value="/login",method=RequestMethod.GET)
@ResponseBody
public String getting(HttpServletResponse response) throws IOException{
//设置成功时的响应码
response.setStatus(606);
//请求重定向
response.sendRedirect("http://www.baidu.com");
return "<div>Hello</div>";
} /**
* @param user 从体中获取的json数据(体中的数据与User里面的属性一一对应)
* @param pathValue 从url地址中获取的路径名称
* @param name 从url地址中获取的对应的参数的值(defaultValue表示默认值)
* @param id
* @return
*/
@RequestMapping(value="/login/{pathValue}",method=RequestMethod.POST)
@ResponseBody
public User posting(@RequestBody User user,@PathVariable String pathValue,@RequestParam(defaultValue="tianheng")String name,int id){
return user;
}
}

6.User.java

import java.sql.Date;
import java.util.List;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/**
* 用户实体类
*
*/
@Component
//@ConfigurationProperties(prefix="my",locations = "classpath:application.yml")
//@ConfigurationProperties(prefix="my") //配合测试类@SpringBootTest()使用
//@Entity
//@NamedQueries({
//@NamedQuery(name = "User.findByNameWithNamedQuery",
//query = "select c from User c where c.name = ?1")})
public class User {
//@Id
private int id;
private String name;
private char sex;
private Date birthDate;
private int height;
//@OneToMany(mappedBy="myUser")
private List<ContactBook> contact; public User(){} public User(int id ,String name,char sex,Date birthDate,int height,List<ContactBook> contact){
this.id = id;
this.name = name;
this.sex = sex;
this.birthDate = birthDate;
this.height = height;
this.contact = contact;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public List<ContactBook> getContact() {
return contact;
}
public void setContact(List<ContactBook> contact) {
this.contact = contact;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}}

7.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.maven</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <!-- Java版本,不要在build path里面修改 -->
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.4.0.RELEASE</version>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.4.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.0.RELEASE</version>
</dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>

8.运行RestMain.java文件

9.Spring—-Spring Boot Rest的使用方法

结果:

Spring—-Spring Boot Rest的使用方法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898