Tag: 休息

如何在基于Spring的强类型语言中正确地使用PATCH – example

据我所知: PUT – 用其整个表示更新对象(替换) 修补程序 – 仅使用给定字段更新对象(更新) 我正在使用Spring来实现一个非常简单的HTTP服务器。 当用户想要更新他的数据时,他需要为某个端点(比如: api/user )创建一个HTTP PATCH 。 他的请求体通过@RequestBody映射到DTO,如下所示: class PatchUserRequest { @Email @Length(min = 5, max = 50) var email: String? = null @Length(max = 100) var name: String? = null … } 然后我使用这个类的一个对象来更新(修补)用户对象: fun patchWithRequest(userRequest: PatchUserRequest) { if (!userRequest.email.isNullOrEmpty()) { email = userRequest.email!! } if (!userRequest.name.isNullOrEmpty()) { name […]

在Spring MVC 3中指定HTTP“位置”响应头的首选方法是什么?

在Spring MVC 3中指定HTTP“位置”响应头的首选方法是什么? 据我所知,Spring只会提供一个“位置”来响应重定向(“redirect:xyz”或RedirectView),但是也有一些场景应该和实体一起发送(例如,作为“201创建”的结果)。 恐怕我唯一的选择是手动指定它: httpServletResponse.setHeader("Location", "/x/y/z"); 它是否正确? 有没有更好的方法来解决这个问题?

如何在android中调用Https Rest服务

我正在尝试调用一个应该返回Json的Https服务。 但是,我无法正确设置连接。 我已经使用了这个主题的“正确”答案的代码: 如何在Android中调用https web服务 这里是我的代码exaclty: MainActivity.java import android.content.Context; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import java.io.BufferedReader; import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context con […]

从Rest服务调用中检索流数据

我正在创建一个将使用以下kotlin代码调用的webservice fun submitRequest(request: PKCS10CertificationRequest): String { // Post request to certificate signing server via http. val conn = URL("$server/api/certificate").openConnection() as HttpURLConnection conn.doOutput = true conn.requestMethod = "POST" conn.setRequestProperty("Content-Type", "application/octet-stream") conn.setRequestProperty("Client-Version", clientVersion) conn.outputStream.write(request.encoded) return when (conn.responseCode) { HTTP_OK -> IOUtils.toString(conn.inputStream, conn.charset) HTTP_FORBIDDEN -> throw IOException("Client version $clientVersion is forbidden from accessing permissioning server, please upgrade to […]

Spring启动REST服务:JSON反序列化不起作用

我正在使用spring-boot和Kotlin开发一个REST服务。 (我应该提到这是我第一次同时使用)。我无法让杰克逊使用此代码从POST请求反序列化JSON: @RequestMapping("cloudservice/login/{uuid}", method = arrayOf(RequestMethod.POST)) fun login(@PathVariable(value="uuid")uuid: String, @RequestBody user: CloudServiceUser ) : ResponseEntity<CloudServiceUser> { val cloudServiceFactory : Class<CloudServiceFactory> = cloudServiceRepository.cloudServiceExtensions[UUID.fromString(uuid)] ?: throw InvalidPathVariableException("Invalid UUID.") var token : String try { token = cloudServiceFactory.newInstance().authenticationService.login(user.userId, user.password) } catch (e:Exception ){ throw CloudServiceException(e.message) } return ResponseEntity(CloudServiceUser(userId=user.userId, password = "", token = token),HttpStatus.OK) } 用户对象很简单: data class […]

相同的其他端点具有不同的PathVariable

我试图做两个相同的uri,但不同类型的休息端点。 第一个将通过EAN(Int)进行搜索,第二个将通过id(String)进行搜索。 我能否以某种方式超载端点? 我和Kotlin一起使用Spring Boot @GetMapping("/book/{ean}") fun getABookByEan(@PathVariable ean: Int) : ResponseEntity<*> { repository.getByEan(ean)?.let { return ResponseEntity.status(HttpStatus.OK).body(it) } throw ItemNotFoundException() } @GetMapping("/book/{id}") fun getABookById(@PathVariable id: String) : ResponseEntity<*> { repository.getById(id)?.let { return ResponseEntity.status(HttpStatus.OK).body(it) } throw ItemNotFoundException() } 在此之后,我得到了一个异常,多个方法被映射为相同的端点。 NestedServletException:请求处理失败; 嵌套异常是java.lang.IllegalStateException:为HTTP路径映射的模糊处理程序方法…

异常处理程序不能使用`spring-boot-starter-data-rest`

我最后一次Java / Spring的经验大约在四年前。 我开始用Kotlin学习Spring Boot。 我已经创建了一个像这样的RESTful Web服务(在Kotlin中),它工作正常: @RequestMapping("/authorization") public fun authorization(@RequestParam(value = "network-type", defaultValue = "Facebook") name: String, @RequestParam(value = "oauth-token") oauthToken: String, @RequestParam(value = "oauth-token-secret", required = false) oauthTokenSecret: String?): Authorization { //TODO: Handle other social network types return facebookAuth.authorization(oauthToken) } 现在我无法添加一个异常处理程序,当facebookAuth抛出UnauthorizedException。 我试过了: 我试图在控制器上注册一个异常处理程序方法。 我试过用@ControllerAdvice创建交叉异常顾问类 在这两种情况下,异常都没有映射,而是我得到: 白标签错误页面 此应用程序没有明确的映射/错误,所以你看到这是一个后备。 Sun Oct 25 16:00:43 PHT 2015 […]

KTor还是Spark? 哪些产品已经准备好用于Kotlin网络服务?

我正在使用Kotlin编写一个企业Web服务应用程序。 我现在正在使用KTor,因为它是异步和高性能的。但是正如你所知,Ktor有点年轻,Kotlin的特性“协程”是实验性的! 反正用Ktor是明智的吗?

如何在Kotlin中创建API请求?

我对Kotlin和API非常陌生,找不到使用此语言创建API请求的语法。 我正在创建网站的移动版本,所以我正在使用Android Studio为已建立的后端创建新的用户界面。 创建请求的步骤和语法是什么? 任何帮助深表谢意。

无法使用Kotlin将主体发布到Spark REST API

我试图按照这个Spark教程来使用Kotlin创建一个REST API ,而我正努力在JSON主体上发布数据 我想将这些数据发布到正文中,如下所示: curl -H "Content-Type: application/json" -X POST -d '{"token" : "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vdHJ1c3R5YXBwLmNvbS8iLCJzdWIiOiJ1c2Vycy8xMzAwODE5MzgwIiwiZXhwIjoxNDg5OTgwNTI2fQ.5ZJG9GyhG-OCXg0C510MBFs9EQHdE909s4hpNxnM6LU"}' http://localhost:4567/tokens 但是,这将在我的Kotlin文件第20行中得到一个空值 /** * REST API for getting tokens * Created by juanma on 15/3/17. */ import spark.Spark.get import spark.Spark.post import spark.Spark.* import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper fun main(args: Array<String>) { get("/hello") { req, res -> "Hello World" } path("/tokens") { get("") { req, […]